Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic memory allocation for 3D array [duplicate]

Possible Duplicates:
Malloc a 3-Dimensional array in C?
dynamic allocation/deallocation of 2D & 3D arrays

How can i allocate 3D arrays using malloc?

like image 616
user239468 Avatar asked Mar 13 '10 11:03

user239468


People also ask

How are 3D arrays stored in memory?

The data items in a multidimensional array are stored in the form of rows and columns. Also, the memory allocated for the multidimensional array is contiguous. So the elements in multidimensional arrays can be stored in linear storage using two methods i.e., row-major order or column-major order.

How do you dynamically allocate memory for two dimensional array?

A 2D array can be dynamically allocated in C using a single pointer. This means that a memory block of size row*column*dataTypeSize is allocated using malloc and pointer arithmetic can be used to access the matrix elements.

Can we use dynamic memory allocation in array?

We can create an array of pointers also dynamically using a double pointer. Once we have an array pointers allocated dynamically, we can dynamically allocate memory and for every row like method 2.

What is dynamic memory allocation in array?

Dynamic Memory Allocation is a process in which we allocate or deallocate a block of memory during the run-time of a program. There are four functions malloc(), calloc(), realloc() and free() present in <stdlib. h> header file that are used for Dynamic Memory Allocation in our system.


3 Answers

There are two different ways to allocate a 3D array. You can allocate it either as a 1D array of pointers to a (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));
          }

        }

Sometimes it is more appropriate to allocate the array as a contiguous chunk. You'll find that many existing libraries might require the array to exist in allocated memory. The disadvantage of this is that if your array is very very big you might not have such a large contiguous chunk 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 your array you just use the macro:

ARR(1,0,3) = 4;
like image 93
Il-Bhima Avatar answered Sep 21 '22 23:09

Il-Bhima


This would 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>;
}
like image 36
Duleb Avatar answered Sep 23 '22 23:09

Duleb


array = malloc(num_elem * num_elem * num_elem * sizeof(array_elem));

Why not? :)

like image 37
Roman Dmitrienko Avatar answered Sep 21 '22 23:09

Roman Dmitrienko