# Allocating and deallocating 2D arrays dynamically in C and C++

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 = 1;
ptr = 2;
ptr = 3;

ptr = 4;
ptr = 5;
ptr = 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 = 1;
ptr = 2;
ptr = 3;

ptr = 4;
ptr = 5;
ptr = 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 = 1;
ptr = 2;
ptr = 3;
ptr = 4;
ptr = 5;
ptr = 6;

ptr = 11;
ptr = 12;
ptr = 13;
ptr = 14;
ptr = 15;
ptr = 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]

### Do you want to Learn Modern C++ from best?

We have curated a list of Best C++ Courses, that will teach you the cutting edge Modern C++ from the absolute beginning to advanced level. It will also introduce to you the word of Smart Pointers, Move semantics, Rvalue, Lambda function, auto, Variadic template, range based for loops, Multi-threading and many other latest features of C++ i.e. from C++11 to C++20.

##### Check Detailed Reviews of Best Modern C++ Courses

Remember, C++ requires a lot of patience, persistence, and practice. So, start learning today.

### 1 thought on “Allocating and deallocating 2D arrays dynamically in C and C++”

1. Hi,
very informative and useful sharing.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top