How to deallocate array in c++ – Allocating and Deallocating 2D Arrays Dynamically in C++

How to deallocate array in c++: In the previous article, we have discussed about C++ : How to split a string using String and character as Delimiter?. Let us learn how to Allocate and Deallocate 2D Arrays Dynamically in C++ Program.

Allocating and Deallocating 2D Arrays Dynamically in C++

C++ 2d array dynamic: In this article, we are going to see how we can allocate and deallocate 2D arrays dynamically by using new-delete(heap) and malloc-free combinations .

Both will have a common approach i.e.

  1. 2D array of size [r][c]
  2. An pointer array of int assigned to int** ptr.
  3. Then the entire pointer array is traversed through and the memory is allocated on the heap of size col.

Allocating 2D arrays using new-delete method :

2d array c++ dynamic: We can allocate and deallocate memory by using new( ) and delete[ ].

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

int **twoDimenArrayUsingNew(int r, int c)
{
    int **ptr = new int *[r];
    for (int i = 0; i< r; i++)
    {
        ptr[i] = new int[c];
    }
    return ptr;
}
void deleteTwoDimenArrayUsingDelete(int **ptr, int r, int c)
{
    for (int i = 0; i < r; i++)
    {
        delete[] ptr[i];
    }
    delete[] ptr;
}

//Function to assign values and call the new and delete
void heapNew()
{
    int r = 2, c = 3;

    int **ptr = twoDimenArrayUsingNew(r, c);

    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 < r; i++)
    {
        for (int j = 0; j < c; j++)
        {
            std::cout << ptr[i][j] << " , ";
        }
        std::cout << std::endl;
    }
    deleteTwoDimenArrayUsingDelete(ptr, r, c);
}

int main()
{
    //Allocating the memory on heap using New and delete
    heapNew();

    return 0;
}
Output :
1 , 2 , 3 ,
4 , 5 , 6 ,

Allocating 2D arrays using malloc( )-dealloc( ) method :

Dynamically allocate 2d array c++: We can allocate and deallocate memory by using malloc( ) and free( ).

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

int **twoDimenArrayUsingMalloc(int r, int c)
{
    int **ptr = (int **)malloc(sizeof(int *) * r);
    for (int i = 0; i < r; i++)
    {
        ptr[i] = (int *)malloc(sizeof(int) * c);
    }
    return ptr;
}
void deleteTwoDimenArrayUsingFree(int **ptr, int r, int c)
{
    for (int i = 0; i < r; i++)
    {
        free(ptr[i]);
    }
    free(ptr);
}

//Function to assign values and call the malloc and free
void heapMalloc()
{
    int r = 2, c = 3;
    int **ptr = twoDimenArrayUsingMalloc(r, c);

    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 < r; i++)
    {
        for (int j = 0; j < c; j++)
        {
            std::cout << ptr[i][j] << " , ";
        }
        std::cout << std::endl;
    }

    deleteTwoDimenArrayUsingFree(ptr, r, c);
}

int main()
{
    //Allocating the memory on heap using malloc and free
    heapMalloc();

    return 0;
}
Output :
1 , 2 , 3 ,
4 , 5 , 6 ,

We saw how we can allocate memory using both methods.