Two "references" to dynamic array, why not change both when reallocating?

zack no-way-zack at no-web.de
Wed Nov 11 10:17:09 UTC 2020


I am new to D. Appending to an array can lead to reallocation, 
that's clear. But why is the "reference" b not changed 
accordingly to the new position and still points to "old" memory? 
Why is b not also changed when reallocating array a and the old 
data getting invalid/freed?

auto a = [55,10,20];
auto b = a;
a ~= [99,99,99,99,99,99];
a[0] = 1;
assert(b[0] == 1); // could fail

(similar to p.103-104 in "The D Programming language")


More information about the Digitalmars-d-learn mailing list