I think that this is a bug in gc/compiler
char [] foo() {
char [10] arr = "hello, wor";
return arr[0..4]; // or return arr;
}
void main() {
printf(foo());
}
doesn`t work - it prints some rubbish, so, as i guess, arr is collected
when function 'foo' returns. if I use char [] instead of char[10] all works fine.
Comment #1 by bugzilla — 2006-12-01T02:52:55Z
char[10] arr;
allocates arr on the stack. Returning a pointer to the stack will result in corrupted data.
char[] arr = "string";
will make arr a reference to the static data literal "string", which is in the static data segment and so remains valid when the function exits.
Not a compiler or gc bug.