An unfortunate "misfeature"
Hxal
hxal at freenode.d.channel
Mon Dec 24 04:17:15 PST 2007
0ffh Wrote:
>
> Assertion:
>
> Initialising a stack variable shouldn't allocate heap memory.
>
> Observation:
>
> I have recently got the impression that this code
>
> void bar(int a,int b)
> {
> int[2] arr=[a,b];
> [...]
> }
>
> allocates heap memory, although we are initialising a variable
> which does not end up on the heap, but on the stack (DMD1).
>
> Suggestion:
>
> I'd hope it would be possible to use a different initialisation
> function for assigning array literals to static arrays.
>
> The slightly ambivalent case
>
> void bar(int a,int b)
> {
> auto arr=[a,b];
> [...]
> }
>
> would of course have a well documented default behaviour. Currently
> it's a static array on the stack (not a dynamic array on the heap).
>
> I think this could be called fixing a misfeature.
>
> regards, frank
Personally I'd be happy if array literals were allocated in the read-only data section
and on the stack (for dynamic initializers). There's always the [1, 2, 3].dup syntax for
allocating on the heap. But that kind of change might break a lot of existing code.
More information about the Digitalmars-d
mailing list