Behavior of "auto"
bearophile
bearophileHUGS at lycos.com
Thu Dec 6 15:57:58 PST 2007
To experiment I have modified your code some (this tread probably belongs to D.learn):
import std.gc;
class Test {
int i;
this(int i) {
this.i = i;
printf("%d created\n", this.i); }
~this() { printf("%d destroyed\n", this.i); }
}
void main() {
printf("Part 1 ----------\n");
for (int i = 0; i < 5; i++)
auto t = new Test(i);
printf("Part1 end ----------\n\n");
printf("Part 2 ----------\n");
for (int i = 0; i < 5; i++)
auto scope t = new Test(i+5);
printf("Part 2 end ----------\n\n");
printf("Part3 ----------\n");
foreach(i, _; new byte[5])
auto scope t = new Test(i+10);
printf("Part 3 end ----------\n\n");
printf("Part4 ----------\n");
foreach(i, _; new byte[5])
auto t = new Test(i+15);
printf("Part 4 end ----------\n\n");
std.gc.fullCollect();
}
And now I too don't fully understand the results (DMD v1.024):
Part 1 ----------
0 created
1 created
2 created
3 created
4 created
Part1 end ----------
Part 2 ----------
5 created
5 destroyed
6 created
6 destroyed
7 created
7 destroyed
8 created
8 destroyed
9 created
9 destroyed
Part 2 end ----------
Part3 ----------
10 created
11 created
12 created
13 created
14 created
Part 3 end ----------
Part4 ----------
15 created
16 created
17 created
18 created
19 created
Part 4 end ----------
18 destroyed
17 destroyed
16 destroyed
15 destroyed
3 destroyed
2 destroyed
1 destroyed
0 destroyed
19 destroyed
4 destroyed
I don't see where the 10-14 objects are deallocated (I have added a fullCollect, but it just scrambles the order of the those last deallocations).
Bye,
bearophile
More information about the Digitalmars-d
mailing list