custom memory management

Simon Bürger" <simon.buerger at rwth-aachen.de> Simon Bürger" <simon.buerger at rwth-aachen.de>
Thu Feb 27 14:11:28 PST 2014


On Thursday, 27 February 2014 at 22:04:50 UTC, Namespace wrote:
> A struct is a value type. So it is passed by value and is 
> placed on the stack.
>
> {
>     S s;
> }
>
> S DTor is called at the end of the scope. So you can rely on 
> RAII as long as you use structs.

On the stack yes. But not on the heap:

     S[] s = new S[17];
     s = null;

the GC will collect the memory eventually, but without calling 
any destructor.

On the other hand:

     class C { S s; }
     C c = new c;
     c = null;

in this case, when the gc collects the memory, it will call both 
destrcutors. The one of C as well as of S.


More information about the Digitalmars-d-learn mailing list