In this article we will see how to allocate and deallocate 2D arrays dynamically using new / delete and malloc / free combinations.

Suppose we want to create a 2D array using pointers on heap either using new or malloc. It should function like this,

	int row = 2;
	int col = 3;
	int ** ptr = allocateTwoDimenArrayOnHeapUsingNew(row, col);

Now we should be able to access 2D array through ptr i.e.

	
	ptr[i][j] = 1;

Then we can deallocate the allocated 2D array using following function,

	void destroyTwoDimenArrayOnHeapUsingDelete(int ** ptr, int row, int col)

Now Let’s explore the solution,

To Store a 2D array we need a pointer to pointer i.e.

int** ptr;

Algo to allocate 2D array dynamically on heap is as follows,

1.) 2D array should be of size [row][col].
2.) Allocate an array of int pointers i.e. (int *) of size row and assign it to int ** ptr.
3.) Traverse this int * array and for each entry allocate a int array on heap of size col.


Code to allocate 2D array dynamically on heap using new operator is as follows,

int ** allocateTwoDimenArrayOnHeapUsingNew(int row, int col)
{
	int ** ptr = new int*[row];
	for(int i = 0; i < row; i++)
	{
		ptr[i] = new int[col];
	}
	return ptr;
}

Code to allocate 2D array dynamically on heap using malloc function is as follows,

int ** allocateTwoDimenArrayOnHeapUsingMalloc(int row, int col)
{
	int ** ptr = (int **) malloc(sizeof(int *)*row);
	for(int i = 0; i < row; i++)
	{
		ptr[i] = (int *) malloc(sizeof(int)*col);
	}
	return ptr;
}

Algo to deallocate 2D array from heap is as follows,

1.) Traverse the array in int ** ptr and for each entry deallocate the int array stored in each int * .
2.) Deallocate the array int ** .

Code to deallocate the dynamically allocated 2D array using delete operator is as follows,

void destroyTwoDimenArrayOnHeapUsingDelete(int ** ptr, int row, int col)
{
	for(int i = 0; i < row; i++)
	{
		delete [] ptr[i];
	}
	delete [] ptr;
}

Code to deallocate the dynamically allocated 2D array using free function is as follows,

void destroyTwoDimenArrayOnHeapUsingFree(int ** ptr, int row, int col)
{
	for(int i = 0; i < row; i++)
	{
		free(ptr[i]);
	}
	free(ptr);
}

Complete executable code is as follows,

[code language=”cpp” collapse=”true”]

#include <iostream>
#include <stdlib.h>

int ** allocateTwoDimenArrayOnHeapUsingNew(int row, int col)
{
int ** ptr = new int*[row];
for(int i = 0; i < row; i++)
{
ptr[i] = new int[col];
}
return ptr;
}

int ** allocateTwoDimenArrayOnHeapUsingMalloc(int row, int col)
{
int ** ptr = (int **) malloc(sizeof(int *)*row);
for(int i = 0; i < row; i++)
{
ptr[i] = (int *) malloc(sizeof(int)*col);

}
return ptr;
}
void destroyTwoDimenArrayOnHeapUsingFree(int ** ptr, int row, int col)
{
for(int i = 0; i < row; i++)
{
free(ptr[i]);
}
free(ptr);
}

void destroyTwoDimenArrayOnHeapUsingDelete(int ** ptr, int row, int col)
{
for(int i = 0; i < row; i++)
{
delete [] ptr[i];
}
delete [] ptr;
}

void testbyHeap()
{

int row = 2;
int col = 3;

int ** ptr = allocateTwoDimenArrayOnHeapUsingNew(row, col);

ptr[0][0] = 1;
ptr[0][1] = 2;
ptr[0][2] = 3;

ptr[1][0] = 4;
ptr[1][1] = 5;
ptr[1][2] = 6;

for(int i = 0; i < row; i++)
{
for(int j = 0; j < col; j++)
{
std::cout<<ptr[i][j]<<" , ";
}
std::cout<<std::endl;
}

destroyTwoDimenArrayOnHeapUsingDelete(ptr, row, col);
}

void testbyMalloc()
{

int row = 2;
int col = 3;

int ** ptr = allocateTwoDimenArrayOnHeapUsingMalloc(row, col);

ptr[0][0] = 1;
ptr[0][1] = 2;
ptr[0][2] = 3;

ptr[1][0] = 4;
ptr[1][1] = 5;
ptr[1][2] = 6;

for(int i = 0; i < row; i++)
{
for(int j = 0; j < col; j++)
{
std::cout<<ptr[i][j]<<" , ";
}
std::cout<<std::endl;
}

destroyTwoDimenArrayOnHeapUsingFree(ptr, row, col);
}

int *** allocateThreeDimenArrayOnHeapUsingNew(int row, int col, int z)
{
int *** ptr = new int**[row];

for(int i = 0; i < row; i++)
{
ptr[i] = new int*[col];
for(int j = 0; j < col; j++)
{
ptr[i][j] = new int[z];
}

}

ptr[0][0][0] = 1;
ptr[0][0][1] = 2;
ptr[0][1][0] = 3;
ptr[0][1][1] = 4;
ptr[0][2][0] = 5;
ptr[0][2][1] = 6;

ptr[1][0][0] = 11;
ptr[1][0][1] = 12;
ptr[1][1][0] = 13;
ptr[1][1][1] = 14;
ptr[1][2][0] = 15;
ptr[1][2][1] = 16;

return ptr;
}

int main()
{
testbyHeap();
testbyMalloc();

int *** ptr = allocateThreeDimenArrayOnHeapUsingNew(2,3,2);

for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 3; j++)
{
std::cout<<"(";
for(int k = 0; k < 2; k++)
{
std::cout<<ptr[i][j][k]<<",";
}
std::cout<<"),";
}
std::cout<<std::endl;
}
return 0;
}

[/code]

Subscribe with us to join a list of 2000+ Programmers for weekly newsletter.