How to dynamically allocate a 3D array in C++
Last Updated :
24 Mar, 2021
Prerequisite: Array Basics
In C/C++, multidimensional arrays in simple words as an array of arrays. Data in multidimensional arrays are stored in tabular form (in row major order). Below is the general form of declaring N-dimensional arrays:
Syntax of a Multidimensional Array:
data_type array_name[size1][size2]....[sizeN];
data_type: Type of data to be stored in the array.
Here data_type is valid C/C++ data type
array_name: Name of the array
size1, size2, ..., sizeN: Sizes of the dimensions
3-D arrays are an array of Double dimensional arrays:
Syntax of a 3D array:
data_type array_name[x][y][z];
data_type: Type of data to be stored. Valid C/C++ data type.
For more details on multidimensional and, 3D arrays, please refer to the Multidimensional Arrays in C++ article.
Problem: Given a 3D array, the task is to dynamically allocate memory for a 3D array using new in C++.
Solution: In the following methods, the approach used is to make two 2-D arrays and each 2-D array is having 3 rows and 4 columns with the following values.
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
21 22 23 24
X = No of 2D arrays.
Y = No of rows of each 2D array.
Z = No of columns of each 2D array.
Method 1: using single pointer - In this method, a memory block of size x*y*z is allocated and then the memory blocks are accessed using pointer arithmetic. Below is the program for the same:
C++
// C++ program to dynamically allocate
// the memory for 3D array in C++
// using new operator
#include <iostream>
using namespace std;
// Driver Code
int main()
{
// Dimensions of the 3D array
int x = 2, y = 3, z = 4;
int count = 0;
// Allocate memory blocks
// of size x*y*z
int* a = new int[x * y * z];
// Traverse the 3D array
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
for (int k = 0; k < z; k++) {
// Assign values to the
// memory blocks created
*(a + i * y * z + j * z + k) = ++count;
}
}
}
// Traverse the 3D array again
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
for (int k = 0; k < z; k++) {
// Print values of the
// memory blocks created
cout << *(a + i * y * z + j * z + k) << " ";
}
cout << endl;
}
cout << endl;
}
// Deallocate memory
delete[] a;
return 0;
}
Output: 1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
21 22 23 24
Method 2: using triple pointer - Below is the diagram to illustrate the concept:
Below is the program for the same:
C++
// C++ program to dynamically allocate
// the memory for 3D array in C++
// using new operator
#include <iostream>
using namespace std;
// Driver Code
int main()
{
// Dimensions of the 3D array
int x = 2, y = 3, z = 4;
int count = 0;
// Allocate memory blocks of size
// x i.e., no of 2D Arrays
int*** a = new int**[x];
for (int i = 0; i < x; i++) {
// Allocate memory blocks for
// rows of each 2D array
a[i] = new int*[y];
for (int j = 0; j < y; j++) {
// Allocate memory blocks for
// columns of each 2D array
a[i][j] = new int[z];
}
}
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
for (int k = 0; k < z; k++) {
// Assign values to the
// memory blocks created
a[i][j][k] = ++count;
}
}
}
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
for (int k = 0; k < z; k++) {
// Print values of the
// memory blocks created
cout << a[i][j][k] << " ";
}
cout << endl;
}
cout << 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;
}
Output: 1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
21 22 23 24
Similar Reads
How to dynamically allocate a 2D array in C? Following are different ways to create a 2D array on the heap (or dynamically allocate a 2D array).In the following examples, we have considered 'r' as number of rows, 'c' as number of columns and we created a 2D array with r = 3, c = 4 and the following values 1 2 3 4 5 6 7 8 9 10 11 12 1) Using a
5 min read
How to declare a 2D array dynamically in C++ using new operator Prerequisite: Array BasicsIn C/C++, multidimensional arrays in simple words as an array of arrays. Data in multidimensional arrays are stored in tabular form (in row major order). Below is the general form of declaring N-dimensional arrays: Syntax of a Multidimensional Array: data_type array_name[si
4 min read
How to pass a 2D array as a parameter in C? A 2D array is essentially an array of arrays, where each element of the main array holds another array. In this article, we will see how to pass a 2D array to a function.The simplest and most common method to pass 2D array to a function is by specifying the parameter as 2D array with row size and co
3 min read
Pointer to an Array in C++ Pointers in C++ are variables that store the address of another variable while arrays are the data structure that stores the data in contiguous memory locations. In C++, we can manipulate arrays by using pointers to them. These kinds of pointers that point to the arrays are called array pointers or
6 min read
One Dimensional Arrays in C++ One-dimensional arrays are like a row of boxes where you can store things where each box can hold one item, such as a number or a word. For example, in an array of numbers, the first box might hold 5, the second 10, and so on. You can easily find or change what's in each box by referring to its posi
6 min read