Dynamic memory allocation for 3D array

Possible duplicates:
Malloc 3-dimensional array in C?


dynamic allocation / deallocation of two-dimensional and three-dimensional arrays

How can I allocate 3D arrays using malloc?

+6
c memory-management malloc multidimensional-array
source share
5 answers
array = malloc(num_elem * num_elem * num_elem * sizeof(array_elem)); 

Why not?:)

+1
source share

There are two different ways to select a 3D array. You can select it as a 1D array of pointers to (1D array of pointers to a 1D array). This can be done as follows:

  int dim1, dim2, dim3; int i,j,k; double *** array = (double ***)malloc(dim1*sizeof(double**)); for (i = 0; i< dim1; i++) { array[i] = (double **) malloc(dim2*sizeof(double *)); for (j = 0; j < dim2; j++) { array[i][j] = (double *)malloc(dim3*sizeof(double)); } } 

It is sometimes more appropriate to select an array as a continuous piece. You will find that many existing libraries may require the array to exist in allocated memory. The disadvantage of this is that if your array is very large, you may not have that large contiguous fragment available in memory.

 const int dim1, dim2, dim3; /* Global variables, dimension*/ #define ARR(i,j,k) (array[dim2*dim3*i + dim3*j + k]) double * array = (double *)malloc(dim1*dim2*dim3*sizeof(double)); 

To access the array, you simply use the macro:

 ARR(1,0,3) = 4; 
+13
source share

It will work

 int main() { int ***p,i,j; p=(int ***) malloc(MAXX * sizeof(int **)); for(i=0;i<MAXX;i++) { p[i]=(int **)malloc(MAXY * sizeof(int *)); for(j=0;j<MAXY;j++) p[i][j]=(int *)malloc(MAXZ * sizeof(int)); } for(k=0;k<MAXZ;k++) for(i=0;i<MAXX;i++) for(j=0;j<MAXY;j++) p[i][j][k]=<something>; } 
+4
source share

For a given type T (non-contiguous):

 size_t dim0, dim1, dim2; ... T ***arr = malloc(sizeof *arr * dim0); //type of *arr is T ** if (arr) { size_t i; for (i = 0; i < dim0; i++) { arr[i] = malloc(sizeof *arr[i] * dim1); // type of *arr[i] is T * if (arr[i]) { size_t j; for (j = 0; j < dim1; j++) { arr[i][j] = malloc(sizeof *arr[i][j] * dim2); } } } } 

If you are not working with a very old (pre-C89) implementation, you do not need to give the result of malloc() , and practice is not recommended. If you forget to include stdlib.h or otherwise do not have a prototype for malloc() in scope, the compiler prints it to return an int , and you get a warning like "incompatible type for assignment". If you produce a result, the warning will be suppressed, and there is no guarantee that converting a pointer to int into a pointer will again be meaningful.

0
source share

@Poita_, ok, maybe you're right, but if someone else wants to use a 3-dimensional array allocated in one large chunk, here's how you add normal indexing to it:

 void*** newarray(int icount, int jcount, int kcount, int type_size) { void*** iret = (void***)malloc(icount*sizeof(void***)+icount*jcount*sizeof(void**)+icount*jcount*kcount*type_size); void** jret = (void**)(iret+icount); char* kret = (char*)(jret+icount*jcount); for(int i=0;i<icount;i++) iret[i] = &jret[i*jcount]; for(int i=0;i<icount;i++) for(int j=0;j<jcount;i++) jret[i*jcount+j] = &kret[i*jcount*kcount*type_size+j*kcount*type_size]; return iret; } 
0
source share

All Articles