How to use destroy and free.

Mike Parker aldacron at gmail.com
Wed May 4 08:23:33 UTC 2022


On Wednesday, 4 May 2022 at 05:37:49 UTC, forkit wrote:

>> That's not at all what I said. You don't have to care about 
>> *when* memory is deallocated, meaning you don't have to manage 
>> it yourself.
>
> In any case, I disagree that caring about when memory gets 
> deallocted means you shouldn't be using GC. (or did I get that 
> one wrong too??)
>
> You can have the best of both worlds, surely (and easily).
>
> This (example from first post):
>
> void main(){
>     int[] i = new int[10000];
>
>     import object: destroy;
>     destroy(i);
>     import core.memory: GC;
>     GC.free(GC.addrOf(cast(void *)(i.ptr)));
> }
>

All you're doing here is putting unnecessary pressure on the GC. 
Just use `malloc` and then `free` on `scope(exit)`. Or if you 
want to append to the array without managing the memory yourself, 
then use `std.container.array` instead. That's made for 
deterministic memory management with no GC involvement.


More information about the Digitalmars-d-learn mailing list