Adam D. Ruppe:

int* CreatePermanentInt() {
   int* i = malloc(int.sizeof);
   *i = 5;
   return i;
}

In D malloc needs a cast void -> T*.


In D a wrapper like this could help you avoid some mistakes making the code more DRY (untested):


T* cMalloc(T)() nothrow {
    return cast(T*)malloc(T.sizeof);
}

T* cCalloc(T)(in size_t n) nothrow {
    return cast(T*)calloc(n, T.sizeof);
}


auto i = cMalloc!int;
auto j = cCalloc!int;

Bye,
bearophile

Reply via email to