How to free memory ater use of "new" to allocate it.

Steven Schveighoffer schveiguy at gmail.com
Mon Jul 17 13:50:27 UTC 2023


On 7/16/23 11:41 PM, Alain De Vos wrote:
> The following program prints two different addresses.
> Meaning the new allocates memory until the program dies.
> So the means memory leak by default ?
> 
> 
> ```
> 
> import std.stdio:writefln;
> import object: destroy;
> import core.memory: GC;
> 
> void dofun(){
>      auto a=new int[1000];
>      writefln("%12x",&a);
>      destroy(a);
>      GC.free(a.ptr);
> }
> 
> int main(){
>      dofun();
>      auto b=new int[1000];
>      writefln("%12x",&b);
>      return 0;
> }
> 
> ```
> 

No, what I am trying to explain is that `destroy(a)` is literally 
equivalent to `a = null`.

If you then `free(a)` do you think it does anything?

-Steve


More information about the Digitalmars-d-learn mailing list