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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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++.

// ------------------------- 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;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.