How are 2D static arrays allocated?

Steven Schveighoffer schveiguy at yahoo.com
Tue Nov 4 08:38:30 PST 2008


"Saaa" 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?

Not easily.  A stack frame is a constant size usually.  So allocating a 
runtime-decided amount is not doable.  You could probably write some 
assembly to do it, but generally, static arrays are the only way.

If you do that, they probably have the same performance.

BTW, static arrays implicitly cast to dynamic arrays, so you can create a 
dynamic array that points to a static array, or call a function which takes 
a dynamic array:
void foo(byte[] b) {...}

byte[1024] buf;
byte[] buffer = buf; // points to stack data
foo(buf); // call a function

-Steve 




More information about the Digitalmars-d-learn mailing list