In D, all array literals are dynamic arrays, and are therefore allocated by the GC.
Even in this simple example:
int[3] a = [10, 20, 30];
The array is heap-allocated and then copied into a
.
How are you supposed to initialise a static array without heap-allocation?
You could do it manually:
int[3] a = void;
a[0] = 10;
a[1] = 20;
a[2] = 30;
But this is tedious at best.
Is there a better way?
static const int[3] a = [10, 20, 30];
This will place a constant copy in the data segment. You can create a copy on the stack (that doesn't involve a heap allocation) with a simple assignment (auto copy = a;
).