DEV Community

Praphul Kolte
Praphul Kolte

Posted on • Edited on

Allocating dynamic memory to 3D array

Arrays are commonly used containers to store data for processing. Array stores homogeneous elements in contiguous memory. Array indexes start from zero and we can navigate to any elements by using indexes. Base on structure of array there are 2 type

  • Single or One Dimensional Array : only one dimension

    1D Declaration in C++ : int arr[5];

Image description

  • Multi-dimensional Array : More than one dimension

    2D Declaration in C++ : int arr[3][5];
    Image description

    3D Declaration in C++ : int arr[3][3][5];
    Image description

  • 1D array is collection of elements

  • 2D array is collection of 1D arrays

  • 3D array is collection of 2D arrays

  • similarly we can say nD array is collection of (n-1)D arrays

As per language rules array sizes has to be constants which lead to shortage or excess of memory. Developer can allocate memory dynamically at runtime to avoid memory wastage.

Lets see how to allocate memory for 3D. Why 3D because 3D memory allocation has memory allocation for 1D, 2D and 3D arrays also as 3D arrays is collection of 2D and 2D is collection of 1D.

Lets start with declaring pointer for 3D array. Pointer to represent 3D array int ***p;

Lets create separate C++ function to allocate memory by supplying dimensions. Here n, r and c are dimensions.

allocateMemory function first allocates memory to n number of 2D arrays the allocate for r number of 1D array and then allocate memory for elements Or 1D



int *** allocateMemory(int n, int r, int c)
{
    int ***p = new int**[n]; // for n numbers of 2D arrays

    for(int i=0;i<n;i++)
    {
        p[i] = new int*[r]; // for r numbers of 1D arrays
    }

    for(int i=0;i<n;i++)
    {
        for(int j=0; j< r;j++)
        {
            p[i][j] = new int[c]; // for c numbers of elements
        }
    }
    return p;
}


Enter fullscreen mode Exit fullscreen mode

Lets create separate C++ function to accept array elements from user. This function receives pointer to memory which already allocated to array.



void acceptInput(int ***p, int n, int r, int c )
{
    for(int i =0;i<n;i++)
    {
        for(int j =0;j<r;j++)
        {
            for(int k =0;k< c;k++)
            {
                cin>>p[i][j][k];
            }
        }
    }
}


Enter fullscreen mode Exit fullscreen mode

Lets create separate C++ function to Print Array elements on console.



void printArray(int ***p, int n, int r, int c )
{
    for(int i =0;i<n;i++)
    {
        for(int j =0;j<r;j++)
        {
            for(int k =0;k<c;k++)
             {
                cout<<p[i][j][k]<<" ";
             }
        }
    }
}


Enter fullscreen mode Exit fullscreen mode

Lets create separate C++ function to deallocate memory by supplying dimensions and pointer of allocated memory.



void deallocateMemory(int ***p, int n, int r, int c)
{
    for(int i=0;i<n;i++)
    {
        for(int j=0; j< r;j++)
        {
        delete[] p[i][j];
        }
    }

    for(int i=0;i<n;i++)
    {
    delete[] p[i];
    }

    delete[] p;
}


Enter fullscreen mode Exit fullscreen mode

Now we have created functions to allocate memory , fill data in memory, read memory and finally delete the allocated memory.
Its time to write main function and try calling functions



#include <iostream>
using namespace std;

int main()
{
    int n,r,c;
    int ***p = NULL;
    //Read user input
    cout<<"\n Enter first dimension:"<<endl;
    cin>>n;
    cout<<"\n Enter second dimension:"<<endl;
    cin>>r;
    cout<<"\n Enter third dimension:"<<endl;
    cin>>c;

    //Allocating memory
    p = allocateMemory(n, r, c);

    //Read array elements
    cout<<"\n Enter " << n*r*c << " elements :"<<endl;
    acceptInput(p,n, r, c);

    //Print array elements
    cout<<"\n Array elements are :";
    printArray(p,n, r, c);

    //Deleting/free up memory
    deallocateMemory(p,n, r, c);

    return 0;
}




Enter fullscreen mode Exit fullscreen mode

Sample run output:

Enter first dimension:
2

Enter second dimension:
2

Enter third dimension:
2

Enter 8 elements :
11
22
33
44
55
66
77
88

Array elements are :
11 22 33 44 55 66 77 88

Top comments (0)