Confusion regarding struct lifecycle

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Feb 16 00:18:51 PST 2016


On 02/15/2016 06:09 PM, Matt Elkins wrote:

 > * Where do those first three destroyed Foos come from?

I've printed some more information to come up with the following guess. 
I am not sure whether it is correct or intended. Just a guess...

When a temporary Foo object is moved into the array, the temporary 
object is set to Foo.init. This temporary object lives on the stack. In 
fact, all temporary Foo objects of Foo.this(int) live at the same location.

After Foo(8) is moved into the array and set to Foo.init, now Foo(1) is 
constructed on top of it. For that to happen, first the destructor is 
executed for the first life of the temporary, and so on...

There is one less Foo.init destruction because conceptually the initial 
temporary was not constructed on top of an existing Foo.init but raw memory.

The reason it makes sense to me is the fact that all Foo.init 
destructions happen on memory that is outside of the array:

import std.stdio;

struct Foo
{
     @disable this();
     @disable this(this);

     this(int valueIn) {
         value = valueIn;
         writefln("Foo being constructed: %s at %s", value, &this);
     }
     ~this() {writefln("Foo being destroyed  : %s at %s", value, &this);}

     int value;
}

struct FooList
{
     @disable this();
     @disable this(this);

     this(int)
     {
         writeln("foos.ptr: ", foos.ptr);
         writeln("Before 8");
         foos[0] = Foo(8);
         writeln("Before 1");
         foos[1] = Foo(1);
         writeln("Before 2");
         foos[2] = Foo(2);
         writeln("Before 3");
         foos[3] = Foo(3);
         writeln("Before 4");
         foos[4] = Foo(4);            // <-- Added by Ali
         writeln("After Foo construction");
     }

     Foo[10] foos;
}

unittest
{
     auto fooList = FooList(0);
     writeln("About to lose scope");
}

void main() {
}

Output:

foos.ptr: 7FFF15C01740
Before 8
Foo being constructed: 8 at 7FFF15C01740
Before 1
Foo being constructed: 1 at 7FFF15C016E0
Foo being destroyed  : 0 at 7FFF15C016A8  <-- temporary Foo.init
Before 2
Foo being constructed: 2 at 7FFF15C016E4
Foo being destroyed  : 0 at 7FFF15C016A8  <-- ditto
Before 3
Foo being constructed: 3 at 7FFF15C016E8
Foo being destroyed  : 0 at 7FFF15C016A8  <-- ditto
Before 4
Foo being constructed: 4 at 7FFF15C016EC  <-- Added by Ali
Foo being destroyed  : 0 at 7FFF15C016A8  <-- ditto
After Foo construction
About to lose scope
Foo being destroyed  : 0 at 7FFF15C01764
Foo being destroyed  : 0 at 7FFF15C01760
Foo being destroyed  : 0 at 7FFF15C0175C
Foo being destroyed  : 0 at 7FFF15C01758
Foo being destroyed  : 0 at 7FFF15C01754
Foo being destroyed  : 4 at 7FFF15C01750
Foo being destroyed  : 3 at 7FFF15C0174C
Foo being destroyed  : 2 at 7FFF15C01748
Foo being destroyed  : 1 at 7FFF15C01744
Foo being destroyed  : 8 at 7FFF15C01740

Ali



More information about the Digitalmars-d-learn mailing list