How to declare a 2D array dynamically in C++
In this article, we will learn how to declare the dynamic array in C++. We also learn the initialization of a 2D array using a pointer in C++. Here, we will cover the following topics:
- Details about Dynamic memory allocation.
- About the new and delete operator in C++.
- The creation process of the 2D array in C++.
Dynamic memory allocation
Dynamic memory allocation is a process of allocating memory during the run time with the help of a heap. With the help of dynamic memory allocation, the programmer can perform the memory allocation manually.
In C++, we also use the new and delete operator to assign and free the memory allocation process.
New operator
With the help of the new operator, we can request the heap to assign the memory allocation. If sufficient memory space is available, the heap will create a new variable or new object during the run time of the program. It also returns the pointer value to the newly allocated space.
Syntax:
new data_type;
Syntax for memory allocation:
new data_type[size_of_array];
Delete operator
The dynamically allocated memory needs to be freed as soon as the program is completed. With the help of the delete operator, we can release or deallocate the memory location which is occupied by the variable or the object.
Syntax for deallocation memory of a single object:
delete pointer;
Syntax for deallocation memory of a single object:
delete() pointer;
Dynamically allocate a 2D array in C++
There are some steps to allocate the memory in the 2D array. The steps are as follows: ,
Step-1
First, we need to create the pointer in the pointer variable.
Syntax:
int** arry;
Step-2
Then we need to allocate the memory allocation with the help of a new operator, and the pointer will store the reference value in the array.
Syntax:
arry = new int*[row];
Step-3
We can perform the memory allocation of each row in 2D using the loop concept.
Syntax:
for(int i = 0;i < row;i++) {
arry[i] = new int[col];
}
Step-4
Now, we have to provide the input in the array using the loops.
Syntax:
for(int i = 0;i < row;i++) {
for(int j = 0;j < col;j++) {
cin >> arry[i][j];
}
}
Step-5
Finally, after allocating the memory, we need to release the variable from the memory location.
Syntax:
for(int i = 0; i < row;i++) {
delete[] arry[i];
}
Step-6
After completing the above process, we need to delete the pointer to the array of points.
Syntax:
delete[] arry;
Program to create a 2D array in C++
Example 1:
#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;
arry = new int * [row];
for (i = 0; i < row; i++) {
arry[i] = new int[col];
}
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];
}
}
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;
}
for (i = 0; i < row; i++) {
delete[] arry[i];
}
delete[] arry;
return 0;
}
Output:
Number of Rows:
3
Number of Columns:
3
Enter 9 numbers to the Array
Enter the elements at Row 1 Column 1
1
Enter the elements at Row 1 Column 2
2
Enter the elements at Row 1 Column 3
3
Enter the elements at Row 2 Column 1
4
Enter the elements at Row 2 Column 2
5
Enter the elements at Row 2 Column 3
6
Enter the elements at Row 3 Column 1
7
Enter the elements at Row 3 Column 2
8
Enter the elements at Row 3 Column 3
9
Here is your 2D Array:
1 2 3
4 5 6
7 8 9
Example 2:
#include <iostream>
using namespace std;
int main()
{
int m = 3, n = 4, c = 0;
int* arr = new int[m * n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
*(arr + i * n + j) = ++c;
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cout << *(arr + i * n + j)
<< " ";
}
cout << endl;
}
delete[] arr;
return 0;
}
Output:
1 2 3 4
5 6 7 8
9 10 11 12