[Issue 12548] New: Safer malloc/calloc/realloc wrappers in Phobos

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Apr 8 12:42:09 PDT 2014


https://d.puremagic.com/issues/show_bug.cgi?id=12548

           Summary: Safer malloc/calloc/realloc wrappers in Phobos
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody at puremagic.com
        ReportedBy: bearophile_hugs at eml.cc


--- Comment #0 from bearophile_hugs at eml.cc 2014-04-08 12:42:05 PDT ---
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.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list