www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Multiple Dynamic Array

reply efgee <efgee2003 yahoo.com> writes:
Hi,
as a newbie I have some difficulties with creating and working with multiple
dynamic arrays.

Here is a multiple static array which seems to work fine:
(please tell me if there is anything that can be done better...)

multiple static array test code:

int main (char [] [] args)  {
  static int[64][64][64] A;
  static char[64][64][64] B;
  static int i,j,k,n,x;
  static char c;

  for(n = 1; n <= 100; n ++)
  {
    for(i = 0; i <= 63; i ++)
    {
      for(j = 0; j <= 63; j ++)
      {
        for(k = 0; k <= 63; k++)
        {

          A[i][j][k] = 100; //numeric array write

          x = A[i][j][k];   //numeric array read

          B[i][j][k] = 'A'; //string array write

          c = B[i][j][k];   //string array read

        }
      }
    }
  }

  return 0 ;
}

Here now a dynamic array.
There must be something missing, because it compiles but there is a
ArrayBoundsError on runtime.

multiple dynamic array test code:

int main (char [] [] args)  {
  int i,j,k,n,x;
  int[][][] A;
  char[][][] B;
  char c;

  for(n = 1; n <= 100; n ++)
  {
    for(i = 0; i <= 63; i ++)
    {
      for(j = 0; j <= 63; j ++)
      {
        for(k = 0; k <= 63; k++)
        {

          A[i][j][k] = 100; //numeric array write

          x = A[i][j][k];   //numeric array read

          B[i][j][k] = 'A'; //string array write

          c = B[i][j][k];   //string array read

        }
      }
    }
  }

  return 0 ;
}

Any help is truly appreciated.

Tried to compile it with GDC 0.2 and DMD 0.178


efgee

BTW: Looked for documents for that but all I found was some very simple
example code that didn't explain anything serious.
Is there good documentation for newbies?
Dec 25 2006
next sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"efgee" <efgee2003 yahoo.com> wrote in message 
news:emps0s$50k$1 digitaldaemon.com...
 Here now a dynamic array.
 There must be something missing, because it compiles but there is a
 ArrayBoundsError on runtime.
Yep, you're missing the initializations. The statically-sized arrays are allocated on the stack, so they're like local variables. But by default, dynamically-sized arrays don't point to anything. Or, another way to think of it is that they're dynamically-sized but they always start with a size of 0.
  int[][][] A;
  char[][][] B;
Replace these with: int[][][] A = new int[][][](64, 64, 64); char[][][] B = new char[][][](64, 64, 64);
Dec 25 2006
parent reply efgee <efgee2003 yahoo.com> writes:
Thank you very much, this:

 int[][][] A = new int[][][](64, 64, 64);
 char[][][] B = new char[][][](64, 64, 64);
worked like a charm, with DMD and GDC. How do you guys/gals know all these things, didn't find it anywhere written down... Thanks again efgee
Dec 25 2006
parent Bill Baxter <dnewsgroup billbaxter.com> writes:
efgee wrote:
 Thank you very much, this:
 
 int[][][] A = new int[][][](64, 64, 64);
 char[][][] B = new char[][][](64, 64, 64);
worked like a charm, with DMD and GDC. How do you guys/gals know all these things, didn't find it anywhere written down... Thanks again efgee
The spec is your friend, but it can definitely be tricky to find things there. You might expect that particular one to be documented under "Arrays", but no such luck. That particular one is here: http://www.digitalmars.com/d/expression.html#NewExpression There's a button in the upper right of every spec page that says "Comments". That takes you to a wiki page that anyone can edit. I try to add links from the comments page of where I expected to find some information to where it's actually found (or just add the info itself to the comments page). --bb
Dec 25 2006
prev sibling parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
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
Dec 25 2006
parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
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
Dec 25 2006
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Bill Baxter" <dnewsgroup billbaxter.com> wrote in message 
news:empvht$9vk$1 digitaldaemon.com...

 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.
The new int[][][](64, 64, 64) syntax calls _d_newm, which is defined in dmd/src/phobos/internal/gc/gc.d. It allocates several smaller blocks -- one for each dimension and for each element, and sets up the pointers/lengths for you.
Dec 26 2006
parent Bill Baxter <dnewsgroup billbaxter.com> writes:
Jarrett Billingsley wrote:
 "Bill Baxter" <dnewsgroup billbaxter.com> wrote in message 
 news:empvht$9vk$1 digitaldaemon.com...
 
 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.
The new int[][][](64, 64, 64) syntax calls _d_newm, which is defined in dmd/src/phobos/internal/gc/gc.d. It allocates several smaller blocks -- one for each dimension and for each element, and sets up the pointers/lengths for you.
Thanks. Yeh, I guess that behavior makes the most sense, since given an arbitrary int[][][] foo one would expect to be able to do things like change foo[0].length and actually have memory freed. That would be difficult if everything were all allocated as one block. --bb
Dec 26 2006