Bug 523 – I think this is a GC bug

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2006-11-15T13:37:00Z
Last change time
2014-02-15T13:18:36Z
Assigned to
bugzilla
Creator
theos

Comments

Comment #0 by theos — 2006-11-15T13:37:51Z
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.