• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4992
  • Last Modified:

Allocation/Deallocation of Triple Pointer (char***)

What is the best way to allocate/dealloacte a triple pointer to char. I have the following but think there could be better. I'd rather not use malloc since I have a few more new and deletes for double pointers and single pointers. Best stick with the same I've heard.

char*** my3DArray;
my3DArray = new char**[20];
for( int i = 0; i < 20; i++ )
{
      my3DArray[i] = new char*[iSize];
      for( int j = 0; j < iTLampSize; j++ )
            my3DArray[i][j] = new char[16];
}

Then to delete I use this, but sometimes I get 16 memory leaks in my program. If you can point out my mistake I would be appreciative.

for( int i = 0; i < 20; i++ )
{
      for( int x = 0; x < iSize; x++ ) {
            delete [] TLamp[i][x];
      }
      delete [] TLamp[i];
}
delete [] TLamp;
TLamp = NULL;

0
MatrixDweller
Asked:
MatrixDweller
1 Solution
 
AxterCommented:
Hi MatrixDweller,
Check out the code in the following link:
http://code.axter.com/allocate3darray.c
http://code.axter.com/allocate3darray.h

The code in the above link is C code, but it wouldn't be to hard to change it to C++ code.


David Maisonave :-)
Cheers!
0
 
AxterCommented:
Here's the above code but a 2DArray, and it's a C++ version:

template < typename T >
T **Allocate2DArray( int nRows, int nCols)
{
    T **ppi;
    T *pool;
    T *curPtr;
    //(step 1) allocate memory for array of elements of column

    ppi = new T*[nRows];

    //(step 2) allocate memory for array of elements of each row
    pool = new T [nRows * nCols];

    // Now point the pointers in the right place
    curPtr = pool;
    for( int i = 0; i < nRows; i++)
    {
        *(ppi + i) = curPtr;
         curPtr += nCols;
    }
    return ppi;
}

template < typename T >
void Free2DArray(T** Array)
{
    delete [] *Array;
    delete [] Array;
}

int main()
{
    double **d = Allocate2DArray<double>(10000, 10000);
    d[0][0] = 10.0;
    d[1][1] = 20.0;
    d[9999][9999] = 2345.09;
    Free2DArray(d);
}
0
 
AxterCommented:
I just found my C++ version of the Allocate3DArray function.
The following method does not use malloc, and you can use it to create any type.

template < typename T >
T ***Allocate3DArray( int x, int y, int z)
{
     T *** pppx = new T**[x];
     T **  pool_y = new T*[x*y];
     T *   pool_z = new T[x*y*z];
     for(int i_x = 0; i_x < x; ++i_x){
          pppx[i_x] = pool_y;
          pool_y +=y;
          for(int i_y = 0;i_y < y;++i_y){
               pppx[i_x][i_y] = pool_z;
               pool_z +=z;
          }
     }
    return pppx;
}


template < typename T >
void Free3DArray(T*** Array)
{
    delete [] **Array;
    delete [] *Array;
    delete [] Array;
}
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
novitiateCommented:
char*** my3DArray = NULL;
my3DArray = new char**[20];
for( int i = 0; i < 20; i++ )
{
     my3DArray[i] = new char*[iSize]; //an array of iSize is created but
     for( int j = 0; j < iTLampSize; j++ ) //used till iTLampSize, it should be iSize
          my3DArray[i][j] = new char[16];
}

_novi_
0
 
itsmeandnobodyelseCommented:
You also could use that:

#include <vector>
using namespace std;

const int X = 20;
const int Y = 10;
const int Z =  5;

template <class T, int x, int y, int z>
class ArrXYZ : public vector<vector<vector<T> > >
{
public:
    ArrXYZ() : vector<vector<vector<T> > >(x, vector< vector <T> > (y, vector <T> (z))) {}
};  
// note, the space between > >  is essential

int main(void)
{

    ArrXYZ<char, X, Y, Z>  arr3D;
    char c = arr3D[X-1][Y-1][Z-1];
    for (int x = 0; x < X; ++x)
        for (int y = 0; y < Y; ++y)
            for (int z = 0; z < Z; ++z)
                 arr3D[x][y][z] = x * y * z;


    return 0;
}

Regards, Alex

   
0
 
MatrixDwellerAuthor Commented:
My bad with the typos. iTLampSize should be iSize, TLamp should be my3DArray.
0
 
AxterCommented:
Using the code I posted, you can create your array via following method:
Allocate3DArray<char> TLamp(20, 20, iTLampSize);

Then to free it, you can do the following:
Free3DArray(TLamp);

The method used in Allocate3DArray is more efficient because it only makes three calls to new.
Unlike other similar methods that make multiple calls to new depending on the Y and Z value.

The deallocation method is also more efficient since it only has to make three calls to delete.
0
 
AxterCommented:
Correction:
Using the code I posted, you can create your array via following method:
char ***TLamp = Allocate3DArray<char>(20, 20, iTLampSize);

Then to free it, you can do the following:
Free3DArray(TLamp);
0
 
MatrixDwellerAuthor Commented:
Very handy function Axter. You're right about the efficiency too. That is mainly what I was looking for.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now