C++ question-deallocating pointers

Okay, I know how to allocate an array of pointers, but unfortunatly I haven’t quite figured out how to deallocate at this point.

I can easily dallocate a single pointer, or a pointer to said array.

delete sarray

But when it comes to the pointers to the arrays withen the arrays.

Say

char ** sarray;
sarray=char *sarray[wmax];

then

for(int i=0;i<wmax;++i)
sarray*=char sarray[cmax];

I have my pointer to my array of pointers(which point to arrays of chars).

Now…I’ve tried several ways to delete those pointers to the arrays, but so far nothing is working.

Any suggestions?

Is this what you want:


	for ( int i = 0; i < wmax; i++ )
	{
		delete [] sarray*;
	}
	delete [] sarray;

Or do you have a third level of allocs?

I do on another array I’m using, though that does help.

I decided to start with the 2 dimensional array for simplicity, but suggestions for for 3 dimensional array would be useful as well.

Thanks. I really appreciate it.

The basic idea is the same with a 3-dimensional array:


for (int i = 0; i < wmax; i++)
{
    for (int j = 0; j < zmax; j++)
    {
        delete [] sarray*[j];
    }

    delete [] sarray*;
}

delete [] sarray;

You use a loop to delete all the items inside the array, then delete the array itself.