What's the D way of allocating memory?

monarch_dodra monarchdodra at gmail.com
Wed Aug 7 13:16:26 PDT 2013


On Wednesday, 7 August 2013 at 18:56:40 UTC, Ali Çehreli wrote:
>
> import core.memory;
>
> void main()
> {
>     auto p = GC.malloc(42);
> }
>
> But question to others: I wouldn't want garbage filled memory, 
> right? So I should consider GC.calloc first.
>
> Ali

Depends on why you think garbage is a problem I guess.

If it is because of false pointers, yet you aren't storing any 
pointers, then simply allocate using GC.BlkAttr.NO_SCAN. Then, 
garbage won't be a problem.

Otherwise, yeah, calloc should do the trick. Unfortunatly, calloc 
doesn't return much other than a void*, so depending on what you 
are doing, a qalloc + memset might be better.

Also, keep in mind that 0-initialization is not T.init 
initialization. This means that calloc'ed memory is not 
"initialized" per say, it is just zero'ed memory. An emplace is 
necessary before attempting, say an assignment.


More information about the Digitalmars-d-learn mailing list