How to Dynamically Create a 2D Array in C++?

programming

Summary: In this programming example, we will learn how to declare or initialize a 2d array dynamically in C++ using the new operator.

Prerequisites:

What is Dynamic Memory Allocation?

Dynamic memory allocation is the process of allocating memory space for variables or objects on the heap during runtime.

We use dynamic memory allocation when we don’t know the number of items or amount of space required at the time of writing a program (i.e. during compile-time).

Rather we take the size or number of items as inputs during the execution ( i.e. during run time) and use it to allocate memory on the heap.

How to Create Dynamic 2D Array in C++?

In C++, we can dynamically allocate memory using the malloc(), calloc(), or new operator.

It is advisable to use the new operator instead of malloc() unless using C.

In our example, we will use the new operator to allocate space for the array.

To dynamically create a 2D array:

  1. First, declare a pointer to a pointer variable i.e. int** arr;.
  2. Then allocate space for a row using the new operator which will hold the reference to the column i.e. arr = new int*[row];.
  3. Allocate space for columns using the new operator on each row cell that will hold the actual values of array elements i.e. arr[i] = new int[col];.

We are using the pointer to pointer variable because of the following two reasons:

  • The new operator after the memory allocation process returns a pointer.
  • Array and pointers in C/C++ are kind of similar (they both hold the reference).
Array as reference

Here is the implementation of the steps in C++:

#include <iostream>
using namespace std;

//Function to display the array 
void display(int** a, int r, int c){
    for(int i=0; i<r; i++){
        for(int j=0; j<c; j++){
            cout << a[i][j] << ' ';
        }
        cout << endl;
    }
}

int main()
{
    int** array;
    int row, col, i, j;
    
    cout << "Enter the number of rows" << endl;
    cin >> row;
    cout << "Enter the number of columns" << endl;
    cin >> col;
 
    //Dynamically allocating row space in heap
    array = new int*[row];
    //Dynamically allocating column space in heap
    for(i=0; i<row; i++){
        array[i] = new int[col];
    }
 
    //Taking input in the array
    cout << "Enter "<< (row * col) <<" numbers \n";
    for(i=0; i<row; i++){
        for(j=0; j<col; j++){
            cout << "Enter element at "<< i+1 << " row " << j+1 << " column"<< endl;
            cin >> array[i][j];
        }
    }
 
    //Displaying array
    cout << "Matrix is: \n";
    display(array, row, col);
 
    //Free space after the use of array
    delete [] array;
}

Output:

Enter the number of rows
2
Enter the number of columns
3
Enter 6 numbers of elements
Enter element at 1 row 1 column
5
Enter element at 1 row 2 column
6
Enter element at 1 row 3 column
4
Enter element at 2 row 1 column
1
Enter element at 2 row 2 column
3
Enter element at 2 row 3 column
9
Matrix is: 
5 6 4 
1 3 9

In the above program, the size of the array during compilation is unknown. We take the size as input from the user and allocate the same amount of space in the heap memory for the array.

All this allocation process occurs during the run time of the program, so the process is referred to as dynamic allocation.

In this tutorial, we learned what dynamic memory allocation is and how to dynamically create a 2D array in C++ using the new operator and a pointer to the pointer variable.

2 thoughts on “How to Dynamically Create a 2D Array in C++?”

  1. hello,
    i have a question.
    why do you not deleted the column of dinamic 2D array?
    you just deleted like “delete [] array;”, and not deleted “delete [] array[i]” on for loop.

Leave a Reply

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