Prevent Garbage Collector
bearophile
bearophileHUGS at lycos.com
Sat Jan 4 10:09:21 PST 2014
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
More information about the Digitalmars-d-learn
mailing list