Bug 12548 – Safer malloc/calloc/realloc wrappers in Phobos
Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-04-08T12:42:00Z
Last change time
2016-01-18T06:52:46Z
Assigned to
nobody
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2014-04-08T12:42:05Z
In my D code for various reasons I sometimes need or wants to use C memory allocating functions, the malloc/calloc/realloc.
But in D they force the usage of a cast(), that I prefer to avoid. And often you need to specify the type two times, like this (such code is not DRY, and this leads to bugs):
cast(T*)malloc(typeof(T).sizeof * 10);
So I suggest to add to Phobos three little wrappers around those functions that allow remove those casts (this can't be done with the "alloca" function), that are safer than the C functions:
cMalloc!T(n)
cCalloc!T(n)
cRealloc(ptr, n)
cRealloc doesn't need the type because it's inferred from the given pointer.