Best memory management D idioms

ag0aep6g via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Mar 7 22:42:40 PST 2017


On 03/08/2017 02:15 AM, XavierAP wrote:
> I see the default allocator is the same GC heap used by 'new'. Just for
> my learning curiosity, does this mean that if I theAllocator.make()
> something and then forget to dispose() it, it will be garbage collected
> the same once no longer referenced? And so are these objects traversed
> by the GC?

Yes and yes. GCAllocator.allocate calls core.memory.GC.malloc with does 
pretty much the same thing as the builtin `new`.

One difference might be with preciseness. `new` can take the type into 
account and automatically mark the allocation as NO_SCAN. I'm not sure 
if GCAllocator can do that. Maybe if you use `make`/`makeArray` to make 
the allocations.

> I've also looked at mallocator, [2] can it be used in some way to
> provide an allocator instead of the default theAllocator? As far as I
> can tell mallocator is not enough to implement an IAllocator, is there a
> reason, or where's the rest, am I missing it?

To make an IAllocator, use std.experimental.allocator.allocatorObject. 
The example in the documentation shows how it's done.

https://dlang.org/phobos/std_experimental_allocator.html#.allocatorObject


More information about the Digitalmars-d-learn mailing list