What's the point of static arrays ?

IGotD- nise at nise.com
Thu Jul 9 18:02:02 UTC 2020


On Thursday, 9 July 2020 at 12:12:06 UTC, wjoe wrote:
> ...

Static arrays are great because as already mentioned, they are 
allocated on the stack (unless it is global variable something, 
then it ends up in the data segment or TLS area).

As C/C++ now allows dynamically sized static arrays (for stack 
only), shouldn't D allow this as well.

Now you have to do.

import core.stdc.stdlib;

void myFunc(size_t arraySize)
{
     void *ptr = alloca(arraySize);
     ubyte[] arr = cast(ubyte[])ptr[0..arraySize];

}

it would be nice if we could just write

ubyte[arraySize] just like in C.

Now this operation is not safe, but could be allowed in @system 
code.

Is this the reason that ubyte[arraySize] doesn't create a dynamic 
array with size arraySize?

Now you need to do.

ubyte[] arr;
arr.length = arraySize;

Like ubyte[arraySize] is reserved for the same usage as in C.





More information about the Digitalmars-d-learn mailing list