digitalmars.D.learn - Multiple Dynamic Array
- efgee (62/62) Dec 25 2006 Hi,
- Jarrett Billingsley (10/15) Dec 25 2006 Yep, you're missing the initializations. The statically-sized arrays ar...
- efgee (5/7) Dec 25 2006 worked like a charm, with DMD and GDC.
- Bill Baxter (11/23) Dec 25 2006 The spec is your friend, but it can definitely be tricky to find things
- Bill Baxter (17/20) Dec 25 2006 Dynamic doesn't mean that the memory is automatically allocated for you.
- Bill Baxter (11/36) Dec 25 2006 Heh heh, yeh, do what Jarrett said. I didn't know about that
- Jarrett Billingsley (6/12) Dec 26 2006 The new int[][][](64, 64, 64) syntax calls _d_newm, which is defined in
- Bill Baxter (6/20) Dec 26 2006 Thanks. Yeh, I guess that behavior makes the most sense, since given an...
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
"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
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
efgee wrote:Thank you very much, this: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). --bbint[][][] 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
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
Bill Baxter wrote:efgee wrote: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. --bbHi, 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
"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
Jarrett Billingsley wrote:"Bill Baxter" <dnewsgroup billbaxter.com> wrote in message news:empvht$9vk$1 digitaldaemon.com...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. --bbJarrett, 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