I got this class :
int x;
int y;
int **mat;
MyMatrix::MyMatrix(int a, int b)
:x(a), y(b)
{
int i ,j;
mat = new int*[x];
for (int i = 0; i < x; ++i)
mat[i] = new int[y];
for (i = 0; i < x; ++i){
for (j = 0; j < y; ++j){
mat[i][j] = i + j;
}
}
}
MyMatrix& MyMatrix::add(MyMatrix m){
int i, j;
if (x != m.x || y != m.y) return *this;
for (i = 0; i < x; ++i){
for (j = 0; j < y; ++j){
mat[i][j] += m.mat[i][j];
}
}
return *this;
}
MyMatrix::~MyMatrix()
{
for (int i = 0; i < x; ++i)
delete[] mat[i];
delete mat;
}
and this is my main :
int main(){
MyMatrix m(2, 3);
MyMatrix m1(2, 3);
m.add(m1);
m.print();
}
All is working and prints me the right answer, but there is some allocation problem. I'm working with debugger and see that the program goes twice to the destructor, on the second time the program crushes.
Please explain me whats the problem and why ?