On Tuesday, 18 June 2019 at 12:26:14 UTC, lili wrote:
Is the Dlang fix-length array alloc on stack? when a test
Why, What is the fix-length array memory layout.
You are quite confused...
[...] is an array literal, not a static array. Those aren't the
When you pass a array literal anywhere in your code, it will in
principle be referred as a slice variable. This will not
reallocate the contents. However the slice reference is another
variable that takes up two words of space (see code below).
This slice type is the same variable type that stores dynamic
arrays -- be they allocated or null.
Array literals are not necessarily allocated. The compiler is
free to embed them into the program machine code itself.
If you want a static array, you can just declare it directly e.g.
int[n] arr. Of course you can also generate is out of an array
literal with the staticArray std library function.
PS the layout of D arrays is of course linear and contiguous.
Both static or dynamic, just like C/C++ static arrays or
Hopefully this code makes things clear:
enum lenInts = int.sizeof;
static assert(lenInts == 4);
static assert(lenInts == arrStatic.sizeof);
auto slice = arrStatic;
alias sliceType = typeof(slice);
static assert(is(sliceType == int));
enum lenPointers = size_t.sizeof; // fyi (unsinged) pointers
static assert(ptrdiff_t.sizeof == lenPointers); // fyi signed
static assert(sliceType.sizeof == 2 * lenPointers);
// because a D array reference remembers a pointer (like C) plus
the length (stored in a word-length integer)