Create a 2d array dynamically using pointers in C++

In this tutorial, we will learn how to create a 2D array dynamically using pointers in C++. First, let us understand what is dynamic memory allocation. Memory in the C++ program is divided into parts-

1) Stack- All the variables which are declared inside the function will take up memory from the stack.

2) Heap– It is the unused memory of the program which can be used to allocate memory during runtime.

Dynamic memory allocation in C++ refers to performing memory allocation manually by the users in the heap. This type of memory allocation provides flexibility and we can easily allocate and deallocate memory whenever we need it and whenever we don’t need it anymore.

Now, let us learn how to create a 2D array dynamically using pointers. We can achieve this by following two approaches-

Using single pointers

In this method, we allocate a large block of memory of the desired size, say M x N dynamically and assign it to the pointer. After doing so we use pointer arithmetic for indexing the 2D array which we have created.

#include <iostream>
using namespace std;
//defining the matrix
#define M 4
#define N 5
 

int main()
{
    
    int* a = new int[M * N];//dynamically allocating memory
 
    // assigning values to the allocated memory
    for (int i = 0; i < M; i++)
    {
        for (int j = 0; j < N; j++) {
            *(a + i*N + j) = rand() % 100;//using rand() for getting random values
        }
    }
 
    //displaying the 2D array
    for (int i = 0; i < M; i++)
    {
        for (int j = 0; j < N; j++) {
            cout << *(a + i*N + j) << " ";      
        }
        cout << endl;
    }
 
    // deallocate memory
    delete[] a;
 
    return 0;
}

Output:

83 86 77 15 93
35 86 92 49 21
62 27 90 59 63
26 40 26 72 36

Using an array of pointers to create a 2D array dynamically

In this approach, we can dynamically create an array of pointers of size M and dynamically allocate memory of size N for each row of the 2D array.

Create a 2d array dynamically using pointers in C++

#include <iostream>
using namespace std
 
//defining matrix size
#define M 4
#define N 5
 
int main()
{
    // creating an array of pointers of size M dynamically using pointers
    int** a = new int*[M];
 
    // dynamically allocating memory of size `N` 
    //for each row
    for (int i = 0; i < M; i++) {
        a[i] = new int[N];
    }
 
    // assigning values to the
    //allocated memory
    for (int i = 0; i < M; i++)
    {
        for (int j = 0; j < N; j++) {
            a[i][j] = rand() % 100;
        }
    }
 
    // displaying the 2D array
    for (int i = 0; i < M; i++)
    {
        for (int j = 0; j < N; j++) {
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
 
    // deallocate memory using the delete operator
    for (int i = 0; i < M; i++) {
        delete[] a[i];
    }
    delete[] a;
 
    return 0;
}

Output:

83 86 77 15 93
35 86 92 49 21
62 27 90 59 63
26 40 26 72 36

Thus, we have learned how to declare a 2D array dynamically using pointers.

Leave a Reply

Your email address will not be published.