Archived

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

How do I allocate a multidimensional array with new?

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

Recommended Posts

All I need is a dynamically allocated 3d array. Can someone tell me how to do this. I''ve tried: int ***array = new int[10][10][10]; No luck there. I can''t find any examples in the msdn library. d e e p s k y . s 5 . c o m "If builders made buildings the way programmers write programs, then the first woodpecker to come along would destroy civilization." -Weinberg''s Law

Share on other sites
  int size1 = 10;int size2 = 10;int size3 = 10;int ***threeDarray;threeDarray = (int***)malloc(sizeof(int**)*size1);for(int a = 0; a < 10; a++){ threeDarray[a] = (int**)malloc(sizeof(int*)*size2); for(int b = 0; b < 10; b++){ threeDarray[a][b] = (int*)malloc(sizeof(int)*size3); }}

Share on other sites
ugh! what a pain in the ass. I''d rather just do a 1D
with:

int *array = new int[length * width * height];

I just didn''t want any multiplies slowing my indexing
down.

There has to be a better way.

d e e p s k y . s 5 . c o m

"Who is General Failure and why is he reading my disk?"

Share on other sites
Oops, you wanted to use the new operator. Malloc is just so ingrained in my head its not funny.

  xxx = new int**[size1];for(int b = 0; b < 10; b++){ xxx[b] = new int*[size2]; for(int c = 0; c < 10; c++) { xxx[b][c] = new int[size3]; }}

You do realize that the compiler should change the 3D array into a single array in its optimizations. If you write it as a 1D array, it might save a little time, but not much.

Share on other sites
quote:
Original post by atreyu
I just didn''t want any multiplies slowing my indexing
down.

The compiler will implicitly do multiplies in multi-dimensional arrays. To do them explicitly shouldn''t slow you down.

Mike

Share on other sites
quote:
Original post by Vetinari
[quote]Original post by atreyu
I just didn''t want any multiplies slowing my indexing
down.

The compiler will implicitly do multiplies in multi-dimensional arrays. To do them explicitly shouldn''t slow you down.
Mike

I think that depends on the type of array. It could not possibly use multiplication for the [][][] type because it is not one solid block of mememory, it is an array of arrays of ints. On the other hand I think there is a [,,] type which would use multiplies.

Share on other sites
or:
int (*array)[10][10];
array = new int[10][10][10];

This is the second time this question has been answered in as many weeks--should definitely go in the FAQ (when we get one).

Share on other sites
quote:
Original post by Anonymous Poster
I think that depends on the type of array. It could not possibly use multiplication for the [][][] type because it is not one solid block of mememory, it is an array of arrays of ints. On the other hand I think there is a [,,] type which would use multiplies.

Just to be clear, statically allocated multidimensional arrays are one continuous block of memory, and implicit multiplies are used.

Mike