How should I initialize this multi-dimensional array?
arrays, d, initialization, multidimensional-array
Solution
If you want to declare a jagged array (i.e. where each sub-array may have varying length), then you need to use a loop like you're doing, but that's unnecessary for uniform arrays. This is how you initialize a multi-dimensional array which isn't jagged:
auto arr = new Cell[][][](128, 128, 128);
When you put the numbers between the brackets, you're make it a dynamic array of static arrays. So,
auto arr = new Cell[128][128][128];
declares a dynamic array of a static arrays of length 128 of static arrays of length 128. I guess that it would be useful if you actually needed to do that (which I never have), but it definitely trips up newbies on a regular basis.
Personally, to avoid such issues completely, I just never put the numbers in between the brackets, even when declaring a single dimension array:
auto arr = new Cell[](128);
I find the fact that putting the number between the brackets on the first dimension is treated as a dynamic array while putting numbers in any further levels is treated as a static array to be a poor design choice, and I don't know why that's the way that it is, but that's the way that it is. I can understand wanting to be able to create dynamic arrays of static arrays, but it would have been far more consistent to either disallow `new Cell[128]` or to make it return a `Cell[128]*` rather than a `Cell[]` of length 128, but unfortunately, that's not how it works.
Problem
I have a giant 3-dimensional array that represents my world. It's too big to initialize statically: ``` alias Cell[128][128][128] World; // <-- The compiler points to this line Error: index 128 overflow for static array ``` I tried using `World* world` but it still errors out with the overflow above. So what I have right now is this ugly mess: ``` alias Cell[][][] World; // ... private World world; // ... world.length = WORLD_XDIM; for (uint x = 0; x < world.length; ++x) { world[x].length = WORLD_YDIM; for (uint y = 0; y < world[x].length; ++y) { world[x][y].length = WORLD_ZDIM; } } ``` That works, but it makes me cry a little on the inside. Is there a way to cast the result of calloc to a 3-dimensional array? I've done it with slicing regular arrays, but the 3-D thing is confounding me.