Multiple Dynamic Array

Bill Baxter dnewsgroup at billbaxter.com
Mon Dec 25 17:57:16 PST 2006


Bill Baxter wrote:
> efgee wrote:
>> Hi,
>> as a newbie I have some difficulties with creating and working with 
>> multiple
>> dynamic arrays.
> 
> Dynamic doesn't mean that the memory is automatically allocated for you.
> int[][][] A just basically sets up a base pointer. To actually give it 
> some memory you have to either use new or set the .length property of 
> every dimension.  So you'll need a nested loop like:
> 
>    A.length = 64;
>    for(i = 0; i <= 63; i ++)
>    {
>       A[i].length = 64;
>       for(j = 0; j <= 63; j ++)
>       {
>       A[i][j].length = 64;
>        }
>      }
>    }
> 
> Warning -- above code not tested.
> 
> --bb


Heh heh, yeh, do what Jarrett said.  I didn't know about that
    new int[][][](64, 64, 64)
business.

Jarrett, is that equivalent to doing the nested loop allocations above? 
  Or does it allocate one contiguous chunk and then set the pointers to 
point inside of that?  Just curious, because if you're allocating a huge 
amount of memory that way, then you don't always want it to be 
contiguous, because there may not be a single block of memory available 
that's big enough.

--bb


More information about the Digitalmars-d-learn mailing list