The D language and its compiler is acting correctly here, so the
output you see is correct. All those structs are allocated on the
stack. The first three Test are allocated on the stack. In the
loop it allocates the first struct on the stack, and it gets
destroyed. Then when the successive loop enters, it creates a new
struct, and it uses the same stack space.
See:
import std.stdio;
struct Test {
static Test*[] psObject;
int x;
this(int a) {
x = a;
psObject ~= &this;
}
~this() {
writeln(x);
}
}
void main() {
Test(0);
foreach (i; 1 .. 3)
Test(i);
Test.psObject.writeln();
}
0
1
2
[12FE48, 12FE54, 12FE54]
Bye,
bearophile

The D language and its compiler is acting correctly here, so
the output you see is correct. All those structs are allocated
on the stack. The first three Test are allocated on the stack.
In the loop it allocates the first struct on the stack, and it
gets destroyed. Then when the successive loop enters, it
creates a new struct, and it uses the same stack space.
See:
import std.stdio;
struct Test {
static Test*[] psObject;
int x;
this(int a) {
x = a;
psObject ~= &this;
}
~this() {
writeln(x);
}
}
void main() {
Test(0);
foreach (i; 1 .. 3)
Test(i);
Test.psObject.writeln();
}
0
1
2
[12FE48, 12FE54, 12FE54]
Bye,
bearophile

The D language and its compiler is acting correctly here, so the
output you see is correct. All those structs are allocated on the
stack. The first three Test are allocated on the stack. In the loop
it allocates the first struct on the stack, and it gets destroyed.
Then when the successive loop enters, it creates a new struct, and it
uses the same stack space.

Struct pointers are useful and reliable, but before using them
you need to know the difference between heap and stack, what a
stack frame is, and how structs are handled when they are on the
stack. Learning the basics of such things ideas requires only few
minutes and it will be useful for many years.
Bye,
bearophile

Struct pointers are useful and reliable, but before using them
you need to know the difference between heap and stack, what a
stack frame is, and how structs are handled when they are on
the stack. Learning the basics of such things ideas requires
only few minutes and it will be useful for many years.
Bye,
bearophile