Yey, pointer fun.
Quote: "Those are just guesses so I wouldn't be surprised if it didn't work."
Don't randomly guess.
To delete such an array you just have to do in the inverse of what you did to create it, i.e.:
int sizeX = 5;
int sizeY = 5;
int sizeZ = 5;
// Alloc
int*** my3DArray = new int**[sizeX];
for ( int x = 0; x < sizeX; x++ )
{
my3DArray[x] = new int*[sizeY];
for ( int y = 0; y < sizeY; y++ )
{
my3DArray[x][y] = new int[sizeZ];
for ( int z = 0; z < sizeZ; z++ )
my3DArray[x][y][z] = 0;
}
}
my3DArray[1][2][3] = 1337;
// Dealloc
for ( int x = 0; x < sizeX; x++ )
{
for ( int y = 0; y < sizeY; y++ )
delete [] my3DArray[x][y];
delete [] my3DArray[x];
}
delete [] my3DArray;
my3DArray = 0;
A cleaner method would be to use a single dimension array and wrap it in a class, such as this:
template <class T>
class Array3D
{
public:
Array3D( int sizeX, int sizeY, int sizeZ ) :
sizeX_(sizeX),
sizeY_(sizeY),
sizeZ_(sizeZ)
{
assert( sizeX > 0 && sizeY > 0 && sizeZ > 0 );
int indices = sizeX * sizeY * sizeZ;
data_ = new T[indices];
memset( data_, 0, sizeof(T) * indices );
}
~Array3D()
{
delete [] data_;
}
T& operator () ( int x, int y, int z )
{
assert( x >= 0 && x < sizeX_ ); // OOB?
assert( y >= 0 && y < sizeY_ );
assert( z >= 0 && z < sizeZ_ );
return data_[x + y*sizeX_ + z*sizeX_*sizeY_];
}
private:
T* data_;
int sizeX_,
sizeY_,
sizeZ_;
};
Then you can do nice stuff like:
int sizeX = 5;
int sizeY = 5;
int sizeZ = 5;
Array3D<int> my3DArray( sizeX, sizeY, sizeZ );
my3DArray(1, 2, 3) = 5;