to! converting 1D to 2D array

ed growlercab at gmail.com
Fri Mar 14 15:55:37 PDT 2014


On Friday, 14 March 2014 at 19:24:21 UTC, Chris Williams wrote:

[snip]
> address pointing to an array of Arrays. So with an int[2][2] 
> array, you have a layout like:
>
> @1000 Array(address=1016, length=2)
> @1016 [Array(address=1048, length=2),Array(address=1056, 
> length=2)]
> @1048 [1,2]
> @1056 [3,4]
>
> In this particular case, the data at 1056 is directly following 
> the data at 1048. There's no gap between them, so considering 
> the buffer at 1048 to be a single array of 4 or two arrays of 
> two is inconsequential. But that's no guarantee.

Actually this is guaranteed for static rectangular arrays:

http://dlang.org/arrays#static-arrays (see Rectangular Arrays)
http://wiki.dlang.org/Dense_multidimensional_arrays (Static 
Arrays)

This code below is safe. It is nothing more than a check for 
conformity followed by a memcpy:

int[] a = [1,2,3,4];
int[2][2] b = a;


I might raise a new question asking why this doesn't work as I 
expect:

int[2][2] b;
b=a;


Thanks for your help on this one. It has forced me to drill into 
the internals a bit more, which is always a good thing :)

Cheers,
ed


More information about the Digitalmars-d-learn mailing list