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
  • Re: How to free ... Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
    • Re: How to ... Alain De Vos via Digitalmars-d-learn
      • Re: How... Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
      • Re: How... Steven Schveighoffer via Digitalmars-d-learn
        • Re:... Alain De Vos via Digitalmars-d-learn
          • ... Alain De Vos via Digitalmars-d-learn
            • ... Steven Schveighoffer via Digitalmars-d-learn
          • ... Steven Schveighoffer via Digitalmars-d-learn
        • Re:... Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
          • ... Alain De Vos via Digitalmars-d-learn
            • ... drug007 via Digitalmars-d-learn
              • ... Alain De Vos via Digitalmars-d-learn
                • ... drug007 via Digitalmars-d-learn
              • ... Alain De Vos via Digitalmars-d-learn
    • Re: How to ... Nick Treleaven via Digitalmars-d-learn
      • Re: How... Alain De Vos via Digitalmars-d-learn
        • Re:... Ernesto Castellotti via Digitalmars-d-learn

Reply via email to