Thread overview
Feature request: array stuff
Apr 24, 2008
Janice Caron
Apr 24, 2008
bearophile
May 04, 2008
Lionello Lunesu
May 08, 2008
Koroskin Denis
May 08, 2008
Janice Caron
May 08, 2008
Fawzi Mohamed
April 24, 2008
I would like to be able to allocate a dynamic array on the stack. The following compiles, but does not achieve the desired effect:

    int n = 100;
    scope array = new int[n];

The array is allocated on the heap, not the stack. I would like it to be allocated on the stack.

I would also like to be able to create uninitialised dynamic arrays:

    // on the heap
    auto array = new int[n] = void;

    // on the stack
    scope array = new int[n] = void;

Note that the last one of these can be implemented in machine code merely by decrementing the stack pointer! (Well - almost. array.ptr and array.length would still need to be assigned, but I can live with that).

Obviously, increasing the length of a scope array would have to be illegal!

Finally, I want to be able to increase the length of an array without initialising the new elements:

    array.length = 100 = void;

(The syntax of the last one leaves a bit to be desired, but I can't think of anything better off hand, right now).

Does anyone know if any or all of these are already the subject of enhancement request? Does anyone else want any of these? Is there any reason why I shouldn't add this to bugzilla?
April 24, 2008
Janice Caron Wrote:

> I would like to be able to allocate a dynamic array on the stack.
> I would also like to be able to create uninitialised dynamic arrays:

In the "extra" module in my D libs there are functions for that: http://www.fantascienza.net/leonardo/so/libs_d.zip


> Finally, I want to be able to increase the length of an array without initialising the new elements:

This needs more work...

Bye,
bearophile
May 04, 2008
Janice Caron wrote:
> I would like to be able to allocate a dynamic array on the stack.
> The following compiles, but does not achieve the desired effect:
> 
>     int n = 100;
>     scope array = new int[n];

Maybe this could be made to work as well:

#int n = 100;
#int[n] array;

L.
May 08, 2008
On Sun, 04 May 2008 19:22:29 +0400, Lionello Lunesu <lio@lunesu.remove.com> wrote:

> Janice Caron wrote:
>> I would like to be able to allocate a dynamic array on the stack.
>> The following compiles, but does not achieve the desired effect:
>>      int n = 100;
>>     scope array = new int[n];
>
> Maybe this could be made to work as well:
>
> #int n = 100;
> #int[n] array;
>
> L.

That's not a dynamic allocation, i.e. you cannot do like this:

void foo(int i)
{
   int[i] array;
}

And I don't know of any language, that could do that. Problem is, you have to deal with stack alignment.
C/C++/D use "static stack alignment", i.e. it is mostly manager at compile time.
The only way of manipulating it at run time is a recursion. Asm statements don't help, because
for each "asm { push something; }" statement there is corresponding hidden "asm { pop; }" added.

> Janice Caron wrote:
> I would also like to be able to create uninitialised dynamic arrays:

>    // on the heap
>    auto array = new int[n] = void;
>
>    // on the stack
>    scope array = new int[n] = void;

I would rather stick with capacity member, i.e:

char[] array;
array.length = 100;   // zeroes out 100 elements
array.capacity = 200; // 0..100 are zeroes, 101..200 hold garbage

capacity can be implemented as a member function, rather than a field,
so that (array.sizeof == 8) still true.
May 08, 2008
2008/5/8 Koroskin Denis <2korden@gmail.com>:
>  That's not a dynamic allocation, i.e. you cannot do like this:
>
>  void foo(int i)
>  {
>    int[i] array;
>  }
>
>  And I don't know of any language, that could do that.

C99.

http://www.comeaucomputing.com/techtalk/c99/#variablelengtharrays
May 08, 2008
On 2008-05-08 16:18:04 +0200, "Janice Caron" <caron800@googlemail.com> said:

> 2008/5/8 Koroskin Denis <2korden@gmail.com>:
>> That's not a dynamic allocation, i.e. you cannot do like this:
>> 
>> void foo(int i)
>> {
>> int[i] array;
>> }
>> 
>> And I don't know of any language, that could do that.
> 
> C99.
> 
> http://www.comeaucomputing.com/techtalk/c99/#variablelengtharrays

also fortran can do this :)
Indeed it can be useful.
An ugly side effect is that then the stack can grow a lot and you hit the default ulimit (ifort does it).