What's the D way of allocating memory?

Ali Çehreli acehreli at yahoo.com
Wed Aug 7 11:56:39 PDT 2013


On 08/07/2013 11:40 AM, Gary Willoughby wrote:
> On Wednesday, 7 August 2013 at 17:47:38 UTC, monarch_dodra wrote:
>> If you want to do "semi-manual" memory management, you can use
>> GC.malloc, which works like normal malloc, but is *still* managed by
>> the GC. I'd actually recommend "GC.qalloc" over "GC.malloc": it's the
>> same function, but qalloc actually returns more info, such as the
>> total amount of memory *actually* allocated. This is very useful when
>> you need a buffer of arbitrary size, as you get the most out of your
>> allocation. The memory can also be eagerly marked as unused with
>> "GC.free", although you'll still have to wait for a collect for it to
>> actually be freed.
>
> Interesting. How do i access GC.malloc?

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



More information about the Digitalmars-d-learn mailing list