How are 2D static arrays allocated?

Jarrett Billingsley jarrett.billingsley at gmail.com
Tue Nov 4 08:34:11 PST 2008


On Tue, Nov 4, 2008 at 11:25 AM, Saaa <empty at needmail.com> wrote:
>
>
>>
>> They are also good for allocating buffer space on the stack.
>
> Can dynamic arrays also be on the stack?
> (oversimplified question .. sorry :)
> If so, do they have the same performance then?

You can have a dynamic array that _points_ to the stack, but there is
no general mechanism for allocating a dynamic array directly on the
stack.  You might be able to do something with alloca, but stack space
is usually limited and you wouldn't be able to have very large arrays.

Performance of dynamic arrays is the same no matter where their data
is.  Fixed-size 2D arrays are not faster _because_ they are on the
stack, they just happen to be allocated on the stack.  They are faster
(usually) because they don't need two pointer dereferences.  You can
allocated a fixed-size 2D array on the heap (well.. inside a struct or
class anyway) and it will be just as fast.


More information about the Digitalmars-d-learn mailing list