Best memory management D idioms

Moritz Maxeiner via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Mar 8 04:08:10 PST 2017


On Wednesday, 8 March 2017 at 06:42:40 UTC, ag0aep6g wrote:
> [...]
>
> Yes and yes. GCAllocator.allocate calls core.memory.GC.malloc 
> with does pretty much the same thing as the builtin `new`.

Nitpicking: `new` is typed (i.e. allocation+construction), 
`malloc` and `allocate` are not (only allocation). If you want 
allocation *and* construction with the new Allocator interface, 
you'll want to use the make[1] (and dispose[2] for the reverse 
path) template function; and they are a superset of `new`: You 
cannot, e.g., construct a delegate with new, but you can with 
`make`.

[1] https://dlang.org/phobos/std_experimental_allocator.html#.make
[2] 
https://dlang.org/phobos/std_experimental_allocator.html#.dispose


More information about the Digitalmars-d-learn mailing list