I'm a beginner programmer in C++. Recently, I started working on image processing using C++. I am trying to define and use the following function:
Matrix MVE(Matrix R, double tolerance)
{
int n = R.Y();
Matrix P(n,4);
for (int i = 0; i < n; ++i)
{
P[i][0] = R[i][0];
P[i][1] = R[i][1];
P[i][2] = R[i][2];
P[i][3] = 1.0;
}
Matrix *Q = P.T();
double err = 1.0;
MatVectorCol u(n);
u.Set(1.0 / n);
MatVectorCol Mv(n);
while (err > tolerance)
{
Matrix uM(n, n);
uM.SetDiagonal(u);
Matrix *X = *Q * uM;
Matrix *X1 = *X * P;
Matrix invX(4, 4);
invX = *X1->Inverse();
delete X; // Error here!
delete X1;
}
return invX;
}
but I get this error when I the program executes the line "delete X;" :
Windows has triggered a breakpoint in plappd.exe.
This may be due to a corruption of the heap, which indicates a bug in plappd.exe or any of the DLLs it has loaded.
This may also be due to the user pressing F12 while plappd.exe has focus.
The constructors and operations for Matrix class are define as following:
Matrix::Matrix(int _y, int _x)
{
x = _x;
y = _y;
M = new double [x * y];
buff = new double [x];
memset0_64(M, sizeof(double) * (x * y));
}
Matrix *Matrix::Transpose()
{
Matrix *b = new Matrix(x, y);
for (int i = 0; i < x; ++i)
{
double *b_line_i = (*b)[i];
for (int j = 0; j < y; ++j)
b_line_i[j] = (*this)[j][i];
}
return b;
}
Matrix *Matrix::operator * (const Matrix &m)
{
if (x == m.y)
{
Matrix *b = new Matrix(y, m.x);
for (int i = 0; i < y; ++i)
{
double *b_line_i = (*b)[i];
double *line_i = (*this)[i];
for (int j = 0; j < m.x; ++j)
for (int k = 0; k < x; ++k)
b_line_i[j] += line_i[k] * m(k, j);
}
return b;
}
else
return 0;
}
There is, for sure, something wrong with the code, but I can't figure out where it is and how to solve it.
Thanks in advance.