Sign in to follow this  

Dynamic 3D Arrays

This topic is 3865 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

I'm trying to create a 3D array dynamically but I'm having some trouble with that. I can't figure out what I'm doing wrong here: [CODE] int*** ImageData = new int[IMAGE_SIZE]; for(int i = 0; i < IMAGE_SIZE; i++) { ImageData[i] = new int[IMAGE_SIZE]; for(int j = 0; j < IMAGE_SIZE; j++) { ImageData[i][j] = new int[3]; } } [/CODE] Can someone help?

Share this post


Link to post
Share on other sites
Quote:
Original post by AltecZZ
Can someone help?

Should be something more like

int *** ImageData = new int ** [IMAGE_SIZE];

for( int i = 0; i < IMAGE_SIZE; i++ )
{
ImageData[i] = new int * [IMAGE_SIZE];

for( int j = 0; j < IMAGE_SIZE; j++ )
ImageData[i][j] = new int[3];
}


At the beginning, you have a pointer to an array of double pointer to integers, but then you're just making an array of straight integers. You should be making an array of double pointers to integers. Similar for the next one.

Share this post


Link to post
Share on other sites
What you are doing there is creating a pointer to a pointer to a pointer, and then assigning integers to it, causing an error.
The way to create a dynamic three dimensional array is to call new int** and new int* to create arrays of pointers.

Thus:
[SOURCE]
int main()
{
int *** array;
array = new int**[IMAGE_SIZE]; //Creates an array of pointers to pointers.
for (int i = 0; i < IMAGE_SIZE; ++i)
{
array[i] = new int*[IMAGE_SIZE]; //Creates an array of pointers
for (int j = 0 ; j < IMAGE_SIZE; ++j)
{
array[i][j] = new int[3]; //Creates the array.
}
}

//I'm not sure that this is the correct way to destroy these arrays.
//So if someone could correct this part,that would be amazing.
for (int i = 0; i < IMAGE_SIZE; ++i)
{
for (int j = 0; j < IMAGE_SIZE; ++j)
{
delete [] array[i][j];
}
delete [] array[i];
}

delete [] array;

}

Share this post


Link to post
Share on other sites
Ah, that makes more sense!

What if I wanted them to be unsigned?

I get this error on the first line:

unsigned int *** imageData = unsigned int ** [IMAGE_SIZE];

VS2005 complains about type 'unsigned int' unexpected

Share this post


Link to post
Share on other sites
Quote:
Original post by AltecZZ
I get this error on the first line:

unsigned int *** imageData = unsigned int ** [IMAGE_SIZE];

VS2005 complains about type 'unsigned int' unexpected

Try:

unsigned int *** imageData = new unsigned int ** [IMAGE_SIZE];

Share this post


Link to post
Share on other sites
If you're happy using boost in your project you could use boost::multi_array. All the memory management and exception safety issues have been thought through for you. You're also not restricted to 3D.

http://www.boost.org/libs/multi_array/doc/index.html

Edd

Share this post


Link to post
Share on other sites

This topic is 3865 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this