How do I allocate a multidimensional array with new?

Started by
6 comments, last by atreyu 22 years, 9 months ago
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
Advertisement
  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);	}}  
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?"
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.
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
"Unintentional death of one civilian by the US is a tragedy; intentional slaughter of a million by Saddam - a statistic." - Unknown
quote:Original post by Vetinari
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.
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).
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

This topic is closed to new replies.

Advertisement