Problem with delete[]

Hello, mates.
Here is my problem.
I have this array of pointers - **cell.
cells = new Cell*[columns];
for(int i =0 ; i<columns ; i++)
cells[i] = new Cell[rows];
for(int i = 0 ; i < rows ; i++)
for(int j = 0 ; j < columns ; j++)
cells[j][i].setValue(other.cells[j][i].getValueF());

Then i have this destructor :
Table::~Table()
{
for(int i =0 ; i<columns ; i++)
delete[] cells[i];
delete[] cells;
}

But my program doesn't work properly using it. Do you have any idea where my mistake is ? i guess the problem is with the deletion. Thank you in advance !
That looks fine.

What are your symptoms?
Unfortunatelly none...
It does compile , and starts running normally.
But when i create an object of Table it fails.
Without the destructor it works properly.
I have a copy constructor including the same code as the destructor, and it does the same thing to the program :( (otherwise i'm sure there is the problem)
Ok. That code looks fine, and is pretty much the same as I use.

That would only fail IF you never created the 2D-array before calling the constructor. So if you tried to delete the memory that you had not allocated.

Try defaulting cells = 0; then in your destructor use
1
2
3
if (cells != 0) {
 // delete here
}

You are going to a lot of trouble here re-coding the STL.

If you need a 2D array, of say, int, you use a vector of vectors:

vector<vector<int> > Table;

and you are done.

No delete worries, no leaks, and no cod to manage the Table.
Topic archived. No new replies allowed.