Dynamic Allocation of 2D, 3D array in C

In this article, we will learn how we can allocate memory to 1D, 2D, 3D, or higher dimensional array in c/c++ dynamically.

We will use malloc() to allocate memory.

The code will work in both C and C++.

malloc()

malloc() performs dynamic memory allocation.

malloc() Definition

void *malloc(size_t size);

It accepts one argument size which is the number of bytes we want to allocate.

If the operation is successful, it returns a pointer to the memory block.

If the operation is unsuccessful ( malloc() is unable to find contiguous memory block of required size ), it returns NULL.

Dynamic Allocation of 1D array in C/C++

Syntax

int *p = (int *) malloc( n * sizeof( int ) );

OR

int *p;
p = (int *) malloc( n * sizeof( int ) );

The above code initialize an 1D array of size n.

Example,

#include<stdio.h>
#include<stdlib.h>

int main() {
	int *p = (int *)malloc(n*sizeof(int));;
	int n = 10;
	
	p = (int *)malloc(n*sizeof(int));
	
	for(int i=0;i<n;++i){
		p[i] = i;
	} 
	
	printf("Elements of 1D array: \n");
	for(int i=0;i<n;++i){
		printf("%d ",p[i]);
	}
	
	printf("\n");	
}

Output

dynamic allocation of 1D array using malloc

Dynamic Allocation of 2D array in C/C++

Syntax

// initialize a pointer to a pointer
int** p;

// the following code allocates an array of int* of size n to p
// p now points to an array of int* of size n
// Each p[0 to n-1] points to int*
p = (int**)malloc(n * sizeof(int*));

// the following loop allocates an array of int of size m to p[0 to n-1]
// every p[0 to n-1] now points to an array of int of size m
// Each p[0 to n-1][0 to m-1] points to int
for (i = 0; i < n; ++i) {
        p[i] = (int*)malloc(m * sizeof(int));
}

The above code allocates a 2D integer array of size n * m.

Example,

#include<stdio.h>
#include<stdlib.h>

int main() {

	int** p;
	int i, j;

	int n = 4;
	int m = 5;

	// the following code allocates an array of int* of size n to p
	// p now points to an array of int* of size n
	// Each p[0 to n-1] points to int*
	p = (int**)malloc(n * sizeof(int*));
	
	// the following loop allocates an array of int of size m to p[0 to n-1]
	// every p[0 to n-1] now points to an array of int of size m
	// Each p[0 to n-1][0 to m-1] points to int
	for (i = 0; i < n; ++i) {
	        p[i] = (int*)malloc(m * sizeof(int));
	}

	for (i = 0; i < n; ++i) {
		for (j = 0; j < m; ++j) {
			p[i][j] = j + i * m;
		}
	}

	printf("Elements of 2D array: \n");
	for (i = 0; i < n; ++i) {
		for (j = 0; j < m; ++j) {
			printf("%d ", p[i][j]);
		}
		printf("\n");
	}

	printf("\n");
}

Output

dynamic allocation of 2D array using malloc

Dynamic Allocation of 3D array in C/C++

Syntax

// the following code allocates an array of int** of size n to p
// p now points to an array of int** of size n
// Each p[0 to n-1] points to int**
p = (int*)malloc(n * sizeof(int));

// the following loop allocates an array of int* of size m to p[0 to n-1]
// every p[0 to n-1] now points to an array of int* of size m
// Each p[0 to n-1][0 to m-1] points to int*
for (i = 0; i < n; ++i) {
        p[i] = (int**)malloc(m * sizeof(int*));
}

// the following loop allocates an array of int of size l to p[0 to n-1][0 to m-1]
// every p[0 to n-1][0 to m-1] nows points to an array of int of size l
// Each p[0 to n-1][0 to m-1][0 to l-1] points to int
for (i = 0; i < n; ++i) {
        for (j = 0; j < m; ++j) {
                p[i][j] = (int*)malloc(l * sizeof(int));
        }
}

The above code allocates a 3D array of size n * m * l.

Example,

#include<stdio.h>
#include<stdlib.h>

int main() {

	int*** p;
	int i, j, k;

	int n = 2;
	int m = 4;
	int l = 3;

	// the following code allocates an array of int** of size n to p
	// p now points to an array of int** of size n
	// Each p[0 to n-1] points to int**
	p = (int***)malloc(n * sizeof(int**));

	// the following loop allocates an array of int* of size m to p[0 to n-1]
	// every p[0 to n-1] now points to an array of int* of size m
	// Each p[0 to n-1][0 to m-1] points to int*
	for (i = 0; i < n; ++i) {
		p[i] = (int**)malloc(m * sizeof(int*));
	}
	
	// the following loop allocates an array of int of size l to p[0 to n-1][0 to m-1]
	// every  p[0 to n-1][0 to m-1] nows points to an array of int of size l
	// Each p[0 to n-1][0 to m-1][0 to l-1] points to int
	for (i = 0; i < n; ++i) { 
		for (j = 0; j < m; ++j) {
			p[i][j] = (int*)malloc(l * sizeof(int));
		}
	}
	

	for (i = 0; i < n; ++i) {
		for (j = 0; j < m; ++j) {
			for (k = 0; k < l; ++k) {
				p[i][j][k] = k + j*l + i*m*l;
			}
		}
	}

	printf("Elements of 2D array: \n");
	for (i = 0; i < n; ++i) {
		for (j = 0; j < m; ++j) {
			for (k = 0; k < l; ++k) {
				printf("%d ", p[i][j][k]);
			}
			printf("\n");
		}
		printf("\n");
	}

	printf("\n");
}

Output

dynamic memory allocation of 3D array using malloc c/c++

Note

The above code creates an integer array. To initialize other datatype arrays dynamically, just replace int with any datatype/structure/class. Everything else remains the same.

What to Study Next?

  1. Initialize 2D, 3D vector in C++

References

Dynamic Memory Allocation in C

1 thought on “Dynamic Allocation of 2D, 3D array in C”

  1. The C99 standard added support for pointer to Variable-Length Arrays. This feature simplifies handling nd-array a lot. For example a 3d array can be allocated with a single line:
    “`
    int (*p)[m][l] = calloc(n, sizeof *p);
    “`
    That’s all.

Leave a Comment

Your email address will not be published. Required fields are marked *