Thread overview
An unfortunate "misfeature"
Dec 24, 2007
0ffh
Dec 24, 2007
Hxal
Dec 24, 2007
0ffh
Dec 28, 2007
0ffh
December 24, 2007
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
December 24, 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.

December 24, 2007
Hxal wrote:
> 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.

Actually I don't see how it would break anything, if the compiler uses a
different initialisation function specifically in the case of declaring
and initialising a variable (which is a compromise, but hey! =).

That'd mean to have "int[2] x=[1,2];" call a special initialiser, while
other use-cases for constant array retained the current behaviour, which
can admittedly be argued to be more tentative (probably trying to not
let you shoot your own foot).

BTW probably the compromise could be better, but a deeper analysis of
the possibilities would be needed to find the best practical one.

I must admit that I'd personally prefer the more general and clear cut
(and, alas, disruptive) "[]" vs "[].dup" syntax.

regards, frank
December 28, 2007
Hxal wrote:
> 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.

Well, just in case you are interested, I was curious and patched
Phobos to use a single preallocated buffer for array literals. =)
It seems to work, and I suppose the same could be done for Tango.

regards, frank
December 28, 2007
"0ffh" <frank@frankhirsch.youknow.what.todo.net> wrote in message news:fl1hjm$kvu$1@digitalmars.com...
> Hxal wrote:
>> 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.
>
> Well, just in case you are interested, I was curious and patched Phobos to use a single preallocated buffer for array literals. =) It seems to work, and I suppose the same could be done for Tango.

Now use it in a multithreaded app ;)

Unless you've already synchronized it.