Declare a 2D array in C++ using new operator

In this tutorial, we are going to learn how to declare a 2D array in C++ using new operator.

To begin with, first, let’s understand what are multidimensional arrays. In simple words, we can define multidimensional arrays as an array of arrays. Data in multi-dimensional arrays are stored in tabular form. We can get the size of a multidimensional array by multiplying the size of all the dimensions.

Two-dimensional arrays

2D arrays are the form of multi-dimensional arrays, basically, we can visualize two-dimensional arrays as an array of one-dimensional arrays. They can be thought of as a matrix with rows and columns. 2D arrays of size a, b can be declared in the following manner-

Syntax:

data_type  name_of_array[a][b]; //data_type- type of data to be stored.

What is Dynamic memory allocation?

In dynamic memory allocation, the memory space is allocated for variables and objects on the heap during runtime. There are times when we are not aware in advance of how much memory is required for storing the particular information in a defined variable.

So, in order to solve this issue, we allocate memory at run time within the heap for the variable of the given data type using a special operator in C++ known as  new operator which returns the address of the space allocated.

Once, we are done with the process and no longer require the dynamically allocated memory we can use the delete operator to de-allocate the memory, previously created by the new operator.

Creating 2D array using new and single pointer approach

In this approach, we will be allocating memory block of size a*b and then, access them using pointer arithmetics. So, the code for the following approach is-

#include <bits/stdc++.h>
using namespace std;

int main()
{
  
  int a = 4, b = 4, c = 0; // Dimensions of the 2D array

  int* array = new int[a*b];
  //declaring memory block for size[a*b]
  //using new operator here
  

  for (int i=0; i<a; i++) {
    for (int j=0; j<b; j++) {

      *(array + i * b + j) = ++c;//Assigning values to the memory block
    }
  }
  for (int i=0; i<a;i++) {
    for (int j=0; j<b;j++) {

      cout << *(array + i * b + j)<< " ";//printing the values
    }
    cout << endl;
  }

  delete[] array;//deleting the allocated memory

  return 0;
}

Code Explaination:

int a = 4, b = 4, c = 0; // Dimensions of the 2D array

int* array = new int[a*b];

In this block of C++ code, we are defining the dimensions and declaring memory block for the 2D array using new operator.

for (int i=0; i<a; i++) {
  for (int j=0; j<b; j++) {

    *(array + i * b + j) = ++c;//Assigning values to the memory block
  }
}

Here, we are traversing the 2D array and assigning the values to the memory block.

for (int i=0; i<a;i++) {
  for (int j=0; j<b;j++) {

    cout << *(array + i * b + j)<< " ";//printing the values
  }
  cout << endl;
}

Printing the values of the 2D array.

delete[] array;//deleting the allocated memory

De-allocating the memory using delete operator.

Output :

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

Thus, we have learned to declare a 2D array in C++ using new operator.

Leave a Reply

Your email address will not be published.