How to free memory ater use of "new" to allocate it.
drug007
drug2004 at bk.ru
Mon Jul 17 11:58:58 UTC 2023
17.07.2023 13:17, Alain De Vos пишет:
> The following code works:
>
> ```
> import std.stdio:writefln;
> import object: destroy;
> import core.memory: GC;
> import core.stdc.stdlib: malloc,free;
> import std.typecons;
>
> class C {
> int * pa;
> int [] a;
> // Constructor
> this() {writefln("Called constructor");
> pa=cast(int *)malloc(1000*int.sizeof);
> a=pa[0..1000];
> }
>
> ~this(){
> writefln("Called Destructor");
> free(a.ptr);}
>
> }
>
> void dofun()
> {
> auto x=scoped!(C);
> x.a[3]=5;
> writefln("%12x",&x);
>
> }
> int main(){
> dofun();
> dofun();
> return 0;
>
> }
>
>
> ```
>
Note that you do not use new anymore. You allocate your class instances
on stack and their scope is `dofun()` only
More information about the Digitalmars-d-learn
mailing list