I'm a c++ programmer trying to understand how memory allocation works in D.

I created a struct and added a destructor to it. My understanding is that structs have deterministic destructors - they are called when the struct goes out of scope (unless it is allocated with new).

Now if I put instances of the struct in a fixed size array

data[6] d;
d[3] = data(1, 2, 3);

then the destructor on all the contents is called when the array goes out of scope.

However if I add them to a dynamic array...

data[] d;
d ~= data(1, 2, 3)

Then the destructor appears to be called at some random time later. So it looks like it's the garbage collection that is doing this. That seems to go against the specification of how struct works... I'm not creating the item with "new" and as far as I can tell the array is storing instances of objects, not pointers to objects?

Is my understanding correct?
Is it documented anywhere how memory allocation works for this?

Is a dynamic array in fact storing an array of GC'd pointers to the structs? Or something else...

Reply via email to