static array internal & dangling reference
Picaud Vincent via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Nov 12 02:33:05 PST 2016
Hi all,
Still learning... This time what surprised me is how static
arrays work.
I assume (is it true?) that for efficiency reason static size
arrays like int[10] are on the stack and do not involve dynamic
memory allocation:
First surprise: it is possible to share a static array:
void main() {
int[10] sa;
foreach(int i, ref sa_i;sa){
sa_i=i;
}
writeln("\n vect init sa",sa);
int[] sb=sa[3..6]; // <- did not think it was possible
sb[2]=100;
writeln("\n vect sa ",sa);
writeln("\n vect sb ",sb);
}
which prints:
vect init sa[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
vect sa [0, 1, 2, 3, 4, 100, 6, 7, 8, 9] // <- sa and sb are
really shared
vect sb [3, 4, 100]
Second surprise: I tried to create a dangling reference with:
int[] f()
{
int[10] sa;
foreach(int i, ref sa_i;sa){
sa_i=i;
}
int[] sb=sa;
return sb;
}
void f_test()
{
auto sb=f();
sb[2]=100; // I expected an "invalid access" (it points to
"sa", a local variable)
}
However calling f_test seems ok and valgrind tool does not
complain...
So my questions are:
0/ does I miss something? (in C++ for sure you create a dangling
pointer)
1/ what is the internal mechanism for that? Is the GC involved?
Performance impact?
2/ any link describing this in more details is welcome
Thank you
More information about the Digitalmars-d-learn
mailing list