Thread overview | |||||
---|---|---|---|---|---|
|
November 15, 2006 [Issue 523] New: I think this is a GC bug | ||||
---|---|---|---|---|
| ||||
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. -- |
November 15, 2006 Re: [Issue 523] New: I think this is a GC bug | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail | <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. |
December 01, 2006 [Issue 523] I think this is a GC bug | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail | http://d.puremagic.com/issues/show_bug.cgi?id=523 bugzilla@digitalmars.com changed: What |Removed |Added ---------------------------------------------------------------------------- Status|NEW |RESOLVED Resolution| |INVALID ------- Comment #1 from bugzilla@digitalmars.com 2006-12-01 02:52 ------- 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. -- |
Copyright © 1999-2021 by the D Language Foundation