Dynamic memory allocation


This question is not very specific to C++. How can I dynamically allocate memory to a three dimensional or a two dimensional array ?

Allocation of memory is no problem but, keeping track of the offsets into the array is something which will have to be done programatically. Is my conjecture correct ?

Please reply.

Who is Participating?
chensuConnect With a Mentor Commented:
// ------------------------- 2D -------------------------

int nRow = 3, nCol = 2;

int **ppn = new int *[nRow];

for (int ni = 0; ni < nRow; ni++)
    ppn[ni] = new int[nCol];

// ppn[x][y];

for (ni = 0; ni < nRow; ni++)
    delete []ppn[ni];

delete []ppn;


int (*ppn)[2] = new int[3][2];

// ppn[x][y]

delete []ppn;

// ------------------------- 3D -------------------------

int nXMax = 5, nYMax = 3, nZMax = 2;

int ***pppn = new int **[nXMax];

for (int ni = 0; ni < nXMax; ni++)
    pppn[ni] = new int *[nYMax];

    for (int nj = 0; nj < nYMax; nj++)
        pppn[ni][nj] = new int[nZMax];

// pppn[x][y][z];

for (int ni = 0; ni < nXMax; ni++)
    for (int nj = 0; nj < nYMax; nj++)
        delete []pppn[ni][nj];

    delete []pppn[ni];

delete []pppn;
in C/C++, you could use the realloc() function to dynamically resize the array and repopulate it programmatically. The standard pointer arithmetic applies when you want to reference elements. Arrays are stored in row order in C/C++.

Example :

you have array of aai[4][2] of int
The elements of aai[4][2] are stored in memory in the following order.


*aai is of type int[]. Note that:-

aai[1][2] == *( (aai[1])+2) ==
*(* (aai+1)+2)
and that numerically
   aai == aai[0] == &aai[0][0]


You can use chensus approach, allocating arrays of pointers which means you can use the [][] syntax directly.  

An alternative way is to wrap the whole thing in a class, and let that do the work for you,  the class will need to understand offsets etc, but the user won't.

The following code is an example of how you could create a matrix class that shows how the class could manage offsets for you:

#include <iostream>
using namespace std;

class Matrix2D {
    Matrix2D(int xdim, int ydim)
        : mXDim(xdim), mYDim(ydim)
        mData = new double[xdim*ydim];

    virtual ~Matrix2D() { delete [] mData; }

    class Matrix1D
        friend class Matrix2D;

        double& operator[](size_t j)
        { return mDataPtr[j]; }
        double operator[](size_t j) const
        { return mDataPtr[j]; }
        Matrix1D(double* pData)
            : mDataPtr(pData) {};

        double* mDataPtr;

    Matrix1D operator[](size_t i)
    { return Matrix1D(&mData[i*mXDim]); }
    const Matrix1D operator[](size_t i) const
    { return Matrix1D(&mData[i*mXDim]); }
    double* mData;
    size_t mXDim;
    size_t mYDim;

void main()
    Matrix2D m(3,3);

    for (int i=0; i<3; i++)
        for (int j=0; j<3; j++)
            m[i][j] = i*3+j+1;

    for (i=0; i<3; i++)
        for (int j=0; j<3; j++)
            cout << m[i][j] << endl;
shayadAuthor Commented:
Thanks Chensu !
The examples are very good.
All Courses

From novice to tech pro — start learning today.