468,774 Members | 1,502 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,774 developers. It's quick & easy.

dynamic 3D array memory alllocation

can u plz giv a code in c for allocating a memory for 3D array.
Oct 8 '06 #1
3 14440
tyreld
144 100+
[code]

int ***3d_array;
int i;

3d_array = (int ***)malloc(sizeof(int **) * DEPTH_SIZE);

for (i = 0; i < DEPTH_SIZE; i++)
Oct 9 '06 #2
tyreld
144 100+
Here is one way to do it. Keep in mind that this doesn't initialize any of the array to useful values, and that X_SIZE, Y_SIZE, and Z_SIZE are defined else where.

Expand|Select|Wrap|Line Numbers
  1.  
  2. int ***3d_array;
  3. int i, j;
  4.  
  5. 3d_array = (int ***)malloc(sizeof(int **) * X_SIZE);
  6.  
  7. for (i = 0 ;  i < X_SIZE; i++) {
  8.    3d_array[i] = (int **)malloc(sizeof(int *) * Y_SIZE);
  9.  
  10.    for (j = 0; j < Y_YSIZE; j++)
  11.       3d_array[i][j] = (int *)malloc(sizeof(int) * Z_SIZE);
  12. }
  13.  
  14.  
Oct 9 '06 #3
tyreld
144 100+
The c99 standard also supports dynamic allocations using the standard array allocation scheme. If you have c99 compliant compiler the following should do the same thing simpler.

Expand|Select|Wrap|Line Numbers
  1.  
  2. int 3d_array[X_SIZE][Y_SIZE][Z_SIZE];
  3.  
Oct 9 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Nils | last post: by
8 posts views Thread by Peter B. Steiger | last post: by
11 posts views Thread by D | last post: by
14 posts views Thread by chai | last post: by
reply views Thread by skm376 | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.