Archived

This topic is now archived and is closed to further replies.

3dimensional dynamic array

This topic is 5874 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
how bout this

int *arr;

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


or use sub-classes..... tq

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
for your information...

multi-dimensional array is very slow..
since every later column need to be multiply
to get to the set....

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 columns

for( 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 columns

for( 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

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites