3dimensional dynamic array

Started by
4 comments, last by taybrin 22 years, 5 months ago
How do I go about creating a dynamic 3d array . . . int arr[][][3]; Then later i have . . . arr = new int [w][h][3]; This does not work, complains about zero sized array declarations. i have tried a multitude of other ways of defining it but none work. Anyone have any ideas thanks
"PC Load Letter, what the F*Ck's that" ~Office Space
Advertisement
how bout this

int *arr;

arr=new int[w][h][3];


or use sub-classes..... tq
for your information...

multi-dimensional array is very slow..
since every later column need to be multiply
to get to the set....
Well first of all, I don''t think any compiler allows you to make a dynamic array.

What you could do is use an int pointer.
Next allocate the memory needed for the array.
Then calculate the memory indexs yourself based on the new dimensons.

If the array dimensons change reallocate the new memory needed.

And, if you know C++ create a dynamic array class to do this for you.
Since you declared your variable as an array, you cannot assign a new address to it:
  int foo[3];...foo = new int[3];  

is forbidden.

The solution : declare your variable as a pointer (yep, can't live without those).

  int* foo;...foo = new int[3];  


If you want a column of them :

  int i;int **foo = new (int*)[h]for( i = 0; i < h; ++i ) foo[i] = new int[3];  


And, by extension, for a 3D array:

  int i,j;int ***foo = new (int**)[w];   // create the vector of columnsfor( i = 0; i < w; ++i ){   foo[w] = new (int*)[h];     // create a column   for( j = 0; j < h; ++j )      foo[w][h] = new int[3];  // create a cell}  


Now, foo points to an array of columns, foo[x] points to column x and foo[x][y] points to element (x,y).

Of course you have to be careful with your cleanup or you will get a nifty memory leak.

  int i,j;for ( i = 0; i < w; ++i ){  for( j = 0; j < h; ++j )    delete[] foo[i][j];     // delete a cell  delete[] foo[i];          // once done, delete the column}delete[] foo;               // delete the vector of columns  


Yet another solution, is to use std::vector, there is much less bookkeeping (and after all, that is what they have been designed for).

  #include <vector>typedef int cell_t[3];typedef std::vector<cell_t> column_t;typedef std::vector<column_t> array_t;int i;array_t foo(w);         // make an array of w columnsfor( i = 0; i < w; ++i )     foo[i].resize(h);   // set the size of each column to be h.  



calling std::vector::resize() to make the array bigger or smaller will take care of the memory problems.



Edited by - Fruny on November 16, 2001 4:47:00 AM
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
A quick and dirty solution:

  const int w = 2;const int h = 4;int* array;void Array_Create(){  array = new int[3 * w * h];}void Array_SetValue(int x, int y, int z, int val){  *(array + x + y * w + z * w * h) = val;}int Array_GetValue(int x, int y, int z){  return *(array + x + y * w + z * w * h);}void Array_Destroy(){  delete[] array;}  

This topic is closed to new replies.

Advertisement