digitalmars.D.bugs - [Issue 523] New: I think this is a GC bug
- d-bugmail puremagic.com (24/24) Nov 15 2006 http://d.puremagic.com/issues/show_bug.cgi?id=523
- Jarrett Billingsley (9/21) Nov 15 2006 You'll get the same thing in C or C++, because in D, as in those languag...
- d-bugmail puremagic.com (15/15) Dec 01 2006 http://d.puremagic.com/issues/show_bug.cgi?id=523
http://d.puremagic.com/issues/show_bug.cgi?id=523 Summary: I think this is a GC bug Product: D Version: 0.175 Platform: PC OS/Version: Windows Status: NEW Severity: normal Priority: P2 Component: DMD AssignedTo: bugzilla digitalmars.com ReportedBy: theos list.ru 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. --
Nov 15 2006
<d-bugmail puremagic.com> wrote in message news:bug-523-3 http.d.puremagic.com/issues/...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.You'll get the same thing in C or C++, because in D, as in those languages, statically-sized arrays are on the stack. When you return the slice of that data, you're hiding from the compiler the fact that you're returning a pointer to a stack variable. You can fix this by using "return arr[0 .. 4].dup;", as that'll copy the data from the stack onto the heap. When you use dynamic arrays, though, they're always allocated on the heap, so it works fine.
Nov 15 2006
http://d.puremagic.com/issues/show_bug.cgi?id=523 bugzilla digitalmars.com changed: What |Removed |Added ---------------------------------------------------------------------------- Status|NEW |RESOLVED Resolution| |INVALID 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. --
Dec 01 2006