multi-dimensional dynamic arrays

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Feb 19 06:26:25 PST 2016


On 2/19/16 8:53 AM, Jay Norwood wrote:
> On Friday, 19 February 2016 at 07:59:29 UTC, Jonathan M Davis wrote:
>> .. Or you could do something really wonky like
>>
>>     auto arr = new int[][2][](5);
>>
>> which would be a dynamic array of length 5 which holds static arrays
>> of length 2 which hold dynamic arrays which are null.
>
> In my case, int [1][][1] ub;, there is only one dynamic dimension, but
> if I try to use .length to change the length, ub.length = 3, the
> compiler doesn't like that.

Try ub[0].length = 3. You are trying to change the length on one of the 
static arrays.

If you had more than 1 as a static dimension, then you would have to 
change the length of *each* of the elements.

Arrays in D, are actually quite simple. Any time you see:

T[]

It's a dynamic array of T. Any time you see:

T[N]

Where N is a compile-time integer, it's a static array of T.

So dissecting your type:

int[1][][1]

So the outer-most T is int[1][]. You have a single instance of this, in 
a static array.

At the next level, T is int[1], where you have a dynamic array of these.

Finally, at the 3rd level, T is int, you have a single element in a 
static array of int.

-Steve


More information about the Digitalmars-d-learn mailing list