Bug 2075 – Spec does not specify how array literals are stored.
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Linux
Creation time
2008-05-06T15:03:00Z
Last change time
2014-02-24T15:32:14Z
Keywords
spec
Assigned to
bugzilla
Creator
tomas
Comments
Comment #0 by tomas — 2008-05-06T15:03:26Z
It seems DMD currently allocates all array literals on the GC heap. The spec however does not state that this is the required behaviour, and frankly I think it's a bad idea not being able to get a stack array without instead declaring a static array temporary explicitly.
Comment #1 by tomas — 2008-05-06T15:04:31Z
Code to see for yourself:
extern(C) int printf(char*, ...);
int[] func()
{
int[] arr = [1,2,3];
return arr;
}
void main()
{
int stack;
int[] arr = func();
printf("%d %d %d\n", arr[0], arr[1], arr[2]);
printf("stack: %p\nwhere? %p\n", &stack, arr.ptr);
int[] arr2 = [1,2,3,4,5];
printf("where? %p\n", arr2.ptr);
}
Comment #2 by bugzilla — 2008-05-22T05:05:23Z
Fixed dmd 1.030 and 2.014
Comment #3 by bruno.do.medeiros+deebugz — 2008-05-28T16:13:13Z
It might also be good to state that each array literal evaluation causes a new array to be allocated. (Unlike string literals for example)