How to dynamically allocate a 2D array in C++

Hello, Coders! In this C++ tutorial, we will learn about dynamic memory allocation, as well as initialize a 2D array in C++ using the pointers and new operator.

Let’s briefly cover up the following topics here:

  • What is dynamic memory allocation
  • What is new and delete operator in C++
  • How to create a 2D array dynamically in C++

Dynamic Memory Allocation in C++

It is the process of allocating the memory at run time within the heap. In this process, the memory allocation is performed manually by the programmer.

In C++ we use new and delete operators to allocate and free the allocated memory respectively in a more efficient way.

  • new operator

The new operator is used to request memory allocation from the heap. If enough memory is available it allocates the memory space to create a new variable or a new object at runtime.

It also returns the pointer to the newly allocated space.

Syntax:

new data_type;

To allocate memory for an array:

new data_type[size_of_array];
  • delete operator

The dynamically allocated memory has to be freed explicitly by the programmer. The delete operator is used to deallocate or release the memory space occupied by the data variable, pointers, or array.

Syntax:

Deallocating memory from a single object:

delete pointer;

Deallocating memory for an array of objects:

delete[] pointer;

Dynamically allocate a 2D array in C++

1. Create a pointer to a pointer variable.

int** arry;

2. Allocate memory using the new operator for the array of pointers that will store the reference to arrays.

arry = new int*[row];

3. By using a loop, we will allocate memory to each row of the 2D array.

for(int i = 0;i < row;i++) {
    arry[i] = new int[col];
}

4. Now, we will give inputs in the array using a loop.

for(int i = 0;i < row;i++) {
    for(int j = 0;j < col;j++) {
          cin >> arry[i][j];
       }
  }

5. Finally, free the allocated memory for each row.

for(int i = 0; i < row;i++) {
    delete[] arry[i];
 }

After this, delete the pointer to the array of pointer:

delete[] arry;

C++ code to create a 2D array

#include <iostream>

using namespace std;

int main() {

  int ** arry;
  int row, col, i, j;

  cout << "Number of Rows:" << endl;
  cin >> row;
  cout << "Number of Columns:" << endl;
  cin >> col;

  //Allocating the row space in heap dynamically
  arry = new int * [row];

  //Allocating the column space in heap dynamically
  for (i = 0; i < row; i++) {
    arry[i] = new int[col];
  }

  //Giving inputs to the array
  cout << "Enter " << (row * col) << " numbers to the Array\n";
  for (i = 0; i < row; i++) {
    for (j = 0; j < col; j++) {
      cout << "Enter the elements at Row " << i + 1 << " Column " << j + 1 << endl;
      cin >> arry[i][j];
    }
  }

  //Display the array
  cout << "Here is your 2D Array:" << endl;
  for (i = 0; i < row; i++) {
    for (j = 0; j < col; j++) {
      cout << arry[i][j] << ' ';
    }
    cout << endl;
  }

  //Free the memory after the use of array
  for (i = 0; i < row; i++) {
    delete[] arry[i];
  }

  delete[] arry;

  return 0;
}

Output:

Number of Rows:
3
Number of Columns:
2
Enter 6 numbers to the Array
Enter the element at Row 1 Column 1
8
Enter the element at Row 1 Column 2
6
Enter the element at Row 2 Column 1
4
Enter the element at Row 2 Column 2
5
Enter the element at Row 3 Column 1
11
Enter the element at Row 3 Column 2
77
Here is your 2D Array:
8 6
4 5
11 77

I hope this article has helped you understand the concept of initializing the 2D array dynamically in C++.

Keep Coding!!!

You can also read, Dynamic memory allocation in C++

Leave a Reply

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