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

Alain De Vos devosalain at ymail.com
Mon Jul 17 03:58:13 UTC 2023


Maybe code above works when you enforce an Garbage-collection-run 
?

Code below works fine. So you cannot use "new" but must use 
malloc?

```

import std.stdio:writefln;
import object: destroy;
import core.memory: GC;
import core.stdc.stdlib: malloc,free;


void dofun(){
    auto pa=cast(int *)malloc(1000*int.sizeof);
    writefln("%12x",pa);
    auto a=pa[0..1000];
    free(a.ptr);
}

int main(){
     dofun();
    auto pb=cast(int *)malloc(1000*int.sizeof);
    writefln("%12x",pb);
    auto b=pb[0..1000];
    free(b.ptr);
    return 0;

}

```


More information about the Digitalmars-d-learn mailing list