Dynamic memory allocation in C++ for 2D and 3D array
This post will discuss dynamic memory allocation in C++ for multidimensional arrays.
1. Single Dimensional Array
The following is a simple example demonstrating dynamic memory allocation in single-dimensional arrays.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <iostream> #define N 10 // Dynamically allocate memory for 1D Array in C++ int main() { // dynamically allocate memory of size `N` int* A = new int[N]; // assign values to the allocated memory for (int i = 0; i < N; i++) { A[i] = i + 1; } // print the 1D array for (int i = 0; i < N; i++) { std::cout << A[i] << " "; // or *(A + i) } // deallocate memory delete[] A; return 0; } |
2. 2-Dimensional Array
1. Using Single Pointer
In this approach, we simply allocate one large block of memory of size M × N dynamically and assign it to the pointer. Then we can use pointer arithmetic to index the 2D array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#include <iostream> // `M × N` matrix #define M 4 #define N 5 // Dynamically allocate memory for 2D Array in C++ int main() { // dynamically allocate memory of size `M × N` int* A = new int[M * N]; // assign 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; } } // print the 2D array for (int i = 0; i < M; i++) { for (int j = 0; j < N; j++) { std::cout << *(A + i*N + j) << " "; // or (A + i*N)[j]) } std::cout << std::endl; } // deallocate memory delete[] A; return 0; } |
2. Using array of Pointers
We can dynamically create an array of pointers of size M and then dynamically allocate memory of size N for each row, as shown below:

|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
#include <iostream> // `M × N` matrix #define M 4 #define N 5 // Dynamic Memory Allocation in C++ for 2D Array int main() { // dynamically create an array of pointers of size `M` int** A = new int*[M]; // dynamically allocate memory of size `N` for each row for (int i = 0; i < M; i++) { A[i] = new int[N]; } // assign values to the allocated memory for (int i = 0; i < M; i++) { for (int j = 0; j < N; j++) { A[i][j] = rand() % 100; } } // print the 2D array for (int i = 0; i < M; i++) { for (int j = 0; j < N; j++) { std::cout << A[i][j] << " "; } std::cout << std::endl; } // deallocate memory using the delete operator for (int i = 0; i < M; i++) { delete[] A[i]; } delete[] A; return 0; } |
3. 3-Dimensional Array
1. Using Single Pointer
As seen for the 2D array, we allocate memory of size X × Y × Z dynamically and assign it to a pointer. Then we use pointer arithmetic to index the 3D array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
#include <iostream> // `X × Y × Z` matrix #define X 2 #define Y 3 #define Z 4 // Dynamic Memory Allocation in C++ for 3D Array int main() { // dynamically allocate memory of size `X × Y × Z` int* A = new int[X * Y * Z]; // assign values to the allocated memory for (int i = 0; i < X; i++) { for (int j = 0; j < Y; j++) { for (int k = 0; k < Z; k++) { *(A + i*Y*Z + j*Z + k) = rand() % 100; } } } // print the 3D array for (int i = 0; i < X; i++) { for (int j = 0; j < Y; j++) { for (int k = 0; k < Z; k++) { std::cout << *(A + i*Y*Z + j*Z + k) << " "; } std::cout << std::endl; } std::cout << std::endl; } // deallocate memory delete[] A; return 0; } |
2. Using Triple Pointer
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
#include <iostream> // `X × Y × Z` matrix #define X 2 #define Y 3 #define Z 4 // Dynamically allocate memory for 3D Array in C++ int main() { int*** A = new int**[X]; for (int i = 0; i < X; i++) { A[i] = new int*[Y]; for (int j = 0; j < Y; j++) { A[i][j] = new int[Z]; } } // assign values to the allocated memory for (int i = 0; i < X; i++) { for (int j = 0; j < Y; j++) { for (int k = 0; k < Z; k++) { A[i][j][k] = rand() % 100; } } } // print the 3D array for (int i = 0; i < X; i++) { for (int j = 0; j < Y; j++) { for (int k = 0; k < Z; k++) { std::cout << A[i][j][k] << " "; } std::cout << std::endl; } std::cout << std::endl; } // deallocate memory for (int i = 0; i < X; i++) { for (int j = 0; j < Y; j++) { delete[] A[i][j]; } delete[] A[i]; } delete[] A; return 0; } |
That’s all about dynamic memory allocation in C++ for 2D and 3D arrays.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)