gpt4 book ai didi

c++ - C++中堆的损坏

转载 作者:行者123 更新时间:2023-11-30 04:14:19 24 4
gpt4 key购买 nike

我是一名 C++ 初级程序员。最近,我开始使用 C++ 进行图像处理。我正在尝试定义和使用以下函数:

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;
}

但是当我的程序执行“delete X;”这一行时我得到了这个错误:

Windows 已在 plappd.exe 中触发断点。

这可能是由于堆损坏,这表明 plappd.exe 或它加载的任何 DLL 中存在错误。

这也可能是由于用户在 plappd.exe 具有焦点时按了 F12。

Matrix 类的构造函数和操作定义如下:

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;
}

当然,代码有问题,但我不知道它在哪里以及如何解决它。

提前致谢。

最佳答案

您的代码的错误之处在于您过度使用/滥用了 operator new 和动态内存分配。除非必须,否则不要动态分配内存——这很容易出错,如果您是“初学者”,您绝对应该避免这样做。 (除非您使用另一种语言使用动态分配的内存并且知道您在做什么)。

而不是用 new 分配 double , 使用 std::vector<double>或类似的容器。这样您就不必编写析构函数。

如果你的类动态分配内存,它必须有析构函数。 C++ 没有垃圾收集,因此没有人会为您清理烂摊子和释放资源。

如果你的类有析构函数,它应该服从rule of three .您应该定义复制构造函数和赋值运算符。如果不这样做,将导致内存泄漏、未定义的行为和崩溃。

不要从运算符中返回原始指针。不要从运算符返回动态分配的对象(如果必须返回动态分配的对象,请创建单独的函数)。如果您必须返回动态分配的对象,请使用智能指针,这样它们的销毁将是自动的(std::shared_ptrboost::shared_ptr 应该有效)。

传统上,operator*应该按值返回对象。它应该看起来像这样:

Matrix Matrix::operator* (const Matrix &arg1, const Matrix &arg2){...}|

关于c++ - C++中堆的损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18983388/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com