www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - An unfortunate "misfeature"

reply 0ffh <frank frankhirsch.youknow.what.todo.net> writes:
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
Dec 24 2007
parent reply Hxal <hxal freenode.d.channel> writes:
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.
Dec 24 2007
next sibling parent 0ffh <frank frankhirsch.youknow.what.todo.net> writes:
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
Dec 24 2007
prev sibling parent reply 0ffh <frank frankhirsch.youknow.what.todo.net> writes:
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
Dec 27 2007
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"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.
Dec 27 2007