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:
// C++ program to dynamically allocate // the memory for 2D array in C++ // using new operator #include<iostream> using namespace std; // Driver Code int main() { // Dimensions of the 2D array int m = 3, n = 4, c = 0; // Declare a memory block of // size m*n int* arr = new int[m * n]; // Traverse the 2D array for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { // Assign values to // the memory block *(arr + i * n + j) = ++c; } } // Traverse the 2D array for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { // Print values of the // memory block cout << *(arr + i * n + j) << " "; } cout << endl; } //Delete the array created delete[] arr; return 0; }
Output:
1 2 3 4 5 6 7 8 9 10 11 12
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:
// C++ program to dynamically allocate // the memory for 2D array in C++ // using new operator #include<iostream> using namespace std; // Driver Code int main() { // Dimensions of the array int m = 3, n = 4, c = 0; // Declare memory block of size M int** a = new int*[m]; for (int i = 0; i < m; i++) { // Declare a memory block // of size n a[i] = new int[n]; } // Traverse the 2D array for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { // Assign values to the // memory blocks created a[i][j] = ++c; } } // Traverse the 2D array for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { // Print the values of // memory blocks created cout << a[i][j] << " "; } cout << endl; } // Delete the array created for (int i = 0; i < m; i++) // To delete the inner // arrays delete[] a[i]; delete[] a; // To delete the outer array // which contained the pointers // of all the inner arrays return 0; }
Output:
1 2 3 4 5 6 7 8 9 10 11 12
'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.
Conclusion
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 :)