You already know that a 2D array array is an array of arrays. But it is defined at the compiled time. In this article, we learn about dynamically allocating a 2D array in C++ at run time. Let's dive deep to understand the dynamic world!
What is a Dynamic 2D Array in C++?
Can you have dynamic arrays in C++? Yes, C++ provides several ways to create dynamic arrays.
We have 2 types of memory: one which we have already before running the program called stack memory and the other is used for dynamic allocation called heap memory. Stack memory is used at compile time of the C++ program while heap memory is used when we use the 'new' operator somewhere in our program.
So, to create a variable or a data structure dynamically, we must use a 'new' operator. When we deal with the heap memory it always returns an address pointing to a created data structure and then we use a pointer to store that address and to access it from heap memory.
Why use heap memory or dynamic allocation? because heap memory is very efficient as it allows us to use only required memory whereas stack memory first comes and then assigns some part of memory to us.
A dynamic array in C++ is an array that can change its size during runtime. It is just like a normal 2D or multi-dimensional array but it is implemented using pointers and memory allocation functions such as new and delete. The main point is that the size of this array is not fixed at compile time.
These arrays are useful when we don't know the size during compilation or we have to change the size during runtime. But be careful while using them as they require extra careful management to avoid memory leaks.
3 Methods to Dynamically Allocate a 2D Array
Let's now learn about 3 different ways to dynamically allocate a simple 2D array in C++.
Method 1) Single Pointer Method
In this method, a memory block of size M*N is allocated and then the memory blocks are accessed using pointer arithmetic. Below is the program for the same:
First, let's understand this 'new int[m *n]' Here the new operator is used for dynamic allocation and we are creating an array of type int and contiguous allocation of size m * n. This will return us an address as explained above already and stored in a pointer.
Then we are just assigning the values to the address blocks in heap memory using the pointers syntax. 'arr + i * n + j' -> this is the address of the heap block and *(arr + i * n + j) this is the value of that heap block.
Method 2) Using an Array of Pointer
Here an array of pointers is created and then to each memory block. Below is the C++ program to implement it:
'int a' - > This means we are creating a pointer of type int and this pointer will store the address of an array (in this case) of int type pointers. This array of pointers will point to each row in an array in heap memory.
Here we learned about the 'new' operator which is used for the dynamic allocation of 2D arrays in C++ at run time and using the heap memory which is quite more efficient to use. Happy Learning :)