How are 2D static arrays allocated?
ore-sama
spam at here.lot
Fri Nov 7 01:04:06 PST 2008
Jarrett Billingsley Wrote:
> You.. can't actually allocate a static array on the heap using new.
> At least not directly. It's kind of an embarrassing hole in the
> syntax. In fact, I'm not sure if you can even get a static array to
> point onto the heap, since a static array "reference" is treated like
> a value type when assigned to, so if you do something like:
>
> int[3][4] b = (new int[3][4][](1))[0];
>
> The weirdness on the right-hand-side is needed to get around the
> compiler "helpfully" rewriting "new int[3][4]" as "new int[][](3, 4)".
> But this code will simply allocate a static array on the heap, and
> then copy its contents onto the stack. Dumb.
auto rg2=new int[10][20];
writeln(typeof(rg2).stringof); //int[10u][]
writeln(cast(size_t)&rg2[1]-cast(size_t)&rg2[0]); //40
As you can see it allocates contunous int[10][20] and assigns it to dynamic array int[10][]. Which is nearly what topicstarter wants. Well, border checks will be dynamic for the first index.
More information about the Digitalmars-d-learn
mailing list