How to allocate a multi-dimensional (2D/3D) array dynamically using malloc()?
If you're not familiar with dynamic memory allocation, please read Dynamic Memory Allocation∞ page before you read this.
Allocating multidimensional arrays using malloc has intimidated many newbies. This page hopes to clear the most basic doubts.
To allocate a multidimensional array, we have to allocate an array of pointers following which we have to initialize each pointer to a dynamically-allocated row, using a pointer-to-pointer.
Here is how you can allocate a two-dimensional array
#include<stdio.h>
#include<stdlib.h>
int main
()
{
int row,column;
int **p;
/*declaration of p as: pointer-to-pointer of int */
int i, j;
/*Indexing variables*/
printf("Enter the number of rows and columns:\n");
scanf
("%d",&row
);
scanf
("%d",&column
);
p = malloc
( row *
sizeof(*p
) );
/*Allocate integer pointers for the rows */
if(p !=
NULL)
{
for(i =
0; i < row; i++
) /* Loop through each row pointer to allocate memory for columns*/
{
/* Set p[i] pointer to a block of memory for 'column' number of integers */
p
[i
] = malloc
(column *
sizeof **p
);
/*Here, sizeof(**p) is same as sizeof(int) */
if(p
[i
] ==
NULL)
{
printf("Memory allocation failed. Exiting....");
return 1;
}
}
}
else
{
printf("Memory allocation failed. Exiting....");
return 1;
}
/*You can access values in the dynamically allocated array in the same
way you do for other arrayssubscripting rows and columns as below */
for(i=
0;i<row;i++
)
{
for(j=
0;j<column;j++
)
{
p
[i
][j
]=i*column+j
+1;
/* normal-looking array subscripts */
printf("%d", p
[i
][j
]);
}
printf("\n");
}
return 0;
}
The elements of the dynamic array can be accessed with array subscripts like p[i][j], under the hood p[i][j] is translated to *(*(p+i)+j)).
Below is a graphical view of the memory allocation for the 2d array given in the above example code for 3*3 matrix ( 3 rows and 3 columns). Click on the image for an enlarged view.
You can see the pointer expression to access each of the location allocated for the 2d array in the above image. The elements of the 2-dimensional array are contiguous memory locations and we can access this memory using a single subscript notation or in-fact, even without the subscript notation.
Allocating 3D array
If you understand the above program to allocate 2D array, you can allocate arrays of any dimension. However, here's another example showing how to allocate a 3D array(you would almost never need bigger dimension than 3D). You will have to make use of pointer-to-pointer-to-pointer, instead of just pointer-to-pointer. Suppose we have to create a 3D array of order m*n*o, then here is how we can do:
#include<stdio.h>
#include <stdlib.h>
int main
()
{
int ***arr3d;
/* Declaration of arr3d as: pointer-to-pointer-to-pointer of int */
int x, y, z;
int i, j;
printf("Enter the order of the 3d matrix(x, y, z):\n");
scanf
("%d%d%d",&x,&y,&z
);
/* Validate input values */
arr3d=malloc
(x*
sizeof *arr3d
);
/* Allocate 'x' number of pointer-to-pointers to int */
for( i=
0; i<m ; i++
)
{
arr3d
[ i
] = malloc
(y*
sizeof **arr3d
);
/* Allocate 'y' number of pointers to int */
/*Validate malloc's success/failure using the return value*/
for( j=
0; j<n; j++
)
{
arr3d
[ i
][ j
]= malloc
(z*
sizeof ***arr3d
);
/* Allocate 'z' number of ints */
/*Validate malloc's success/failure using the return value*/
}
}
/*Note, the error checking of malloc return value is excluded, look above example to know how to do it.*/
/*For accessing the elements, its similar to any other 3D array... that is arr[i][j][k]; */
return 0;
}
Related Links
comp.lang.c FAQ section on allocating multidimensional array∞.
CategoryArrays
CategoryPointers