2/3 dimensional arrays + comparison page truncated

Digital Mars Dave_member at pathlink.com
Tue Aug 7 11:15:23 PDT 2007


"Daniel White" <twinbee41 at skytopia.com> wrote in message 
news:f96gar$1vjv$1 at digitalmars.com...
> Second thing. How does D allocate for a 2D (or even 3D) array. I tried to 
> look everywhere on the D site for this, and nothing came up. I'm just 
> curious to how it compares with the long-winded C/C++ way of using malloc 
> to create arrays.

Try this:

import std.stdio;
void main()
{
    int[][]   arr2d = new int[][](10,10);
    foreach(d; arr2d)
    {
        foreach(ref x; d)
        {
            x = 2;
        }
    }
    foreach(d; arr2d)
    {
        foreach(x; d)
        {
            writef(x);
        }
        writefln;
    }
    int[][][] arr3d = new int[][][](10,10,10);
    foreach(d2; arr3d)
    {
        foreach(d; d2)
        {
            foreach(ref x; d)
            {
                x = 3;
            }
        }
    }
    foreach(d2; arr3d)
    {
        foreach(d; d2)
        {
            foreach(x; d)
            {
                writef(x);
            }
            writef(" ");
        }
        writefln;
    }
}




More information about the Digitalmars-d mailing list