使用delete会使我的程序崩溃
using delete is crashing my program
当我试图删除distributor上的指针时,我的程序崩溃了,为什么?我不明白我在代码中做错了什么。
我用错新的了吗?
类矩阵:
class Matrix
{
private:
double **_array;
int _rows, _cols;
...
Matrix::Matrix(int rows, int cols)
{
if (rows <= 0 || cols <= 0)
exit(-1);
this->_array = new double*[rows];
for (int i = 0; i < rows; i++)
this->_array[i] = new double[cols];
this->_rows = rows;
this->_cols = cols;
}
问题就在这里:
void Matrix::pow(int power, Matrix& result)
{
/*if (result == NULL)
exit(-1);*/
if (result._cols != this->_cols || result._rows != this->_rows)
exit(-1);
// Can't pow the matrix, return mat of '0' values
if (this->_cols != this->_rows)
{
for (int i = 0; i < result._rows; i++)
for (int j = 0; j < result._cols; j++)
result.setElement(i, j, 0);
return;
}
/*if (power == 0)
result = 1;*/
double sum = 0;
Matrix temp(this->_rows, this->_cols);
// Copy this matrix to result matrix
for (int i = 0; i < this->_rows; i++)
for (int j = 0; j < this->_cols; j++)
result.setElement(i, j, this->_array[i][j]);
// Pow loop
for (int p = 1; p < power; p++)
{
for (int i = 0; i < this->_rows; i++)
for (int j = 0; j < this->_cols; j++)
{
for (int k = 0; k < this->_rows; k++)
sum += this->getElement(i, k) * result.getElement(k, j);
temp.setElement(i ,j ,sum);
sum = 0;
}
// Copy temp array to result array
for (int i = 0; i < this->_rows; i++)
for (int j = 0; j < this->_cols; j++)
result.setElement(i, j, temp.getElement(i, j));
for (int i = temp._rows; i >= 0; i--)
delete[] temp._array[i];
delete[] temp._array;
}
}
Main:
void main()
{
int rows = 3, cols = 3;
Matrix m1(rows, cols);
Matrix other(rows, cols);
Matrix result(rows, cols);
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
{
m1.setElement(i, j, i + j);
other.setElement(i, j, 3 * (i + j + 1));
}
m1.pow(3, result);
}
SetElements:
void Matrix::setElement(int i, int j, double data)
{
if (i < 0 || j < 0)
exit(-1);
if (i >= this->_rows || j >= this->_cols)
exit(-1);
_array[i][j] = data;
}
感谢
1)准备对象不再存在的成员函数的单词是析构函数,而不是distributor。
2) 如果您使用运算符new
的数组形式,那么使用运算符delete
的非数组形式(您就是这样)会产生未定义的行为。请改用运算符delete
的数组形式。
3) 如图所示,您的代码使用了您没有提供的其他函数。其中任何一个,如果实现不正确,都可能对指针执行无效操作,因此可能是导致崩溃的其他原因。
4) 不要麻烦使用动态内存分配(运算符new
等)来处理动态分配的数组。您已经证明了这样做很容易出错。如果您想要动态分配的double
数组,请使用std::vector<double>
。如果需要double
的二维数组,请使用std::vector<std::vector<double> >
。除了不易出错之外,std::vector
还可以正确地释放内存(只要你不写其他破坏内存的代码)。
在Matrix
中,您可以分配新的数组:
...
this->_array = new double*[rows]; // <== array of pointers
for (int i = 0; i < rows; i++)
this->_array[i] = new double[cols]; // <== arrays of doubles
...
但是在你的析构函数中你删除了元素。你必须纠正这一点:每次你定位一个数组(new []
),你都必须删除该数组(delete []
),否则你会得到未定义的行为(例如崩溃):
for (int i = 0; i < _rows; i++)
delete[] _array[i]; // <== delete array, not element!!
delete[] _array; // <== delete array, not element!!
注意:每次你想使用new/delete时,你都应该问问自己,是否不值得考虑使用向量。这是非常容易的,因为这个在线演示显示您的代码返工
编辑:您的pow()
问题:
在这里创建矩阵对象temp
:
Matrix temp(this->_rows, this->_cols);
该对象将在函数pow()
结束时自动销毁。这意味着它的析构函数将调用所有必需的delete[]
。
问题是,在函数结束时,您手动完成析构函数的工作,因此您需要delete[]
temp的数组。然后析构函数尝试删除已经删除的对象。这是未定义的行为,因此导致了崩溃!
您所要做的就是去掉pow()
的最后3行以及它们所包含的不必要的delete[]
。
- 程序崩溃并显示"std::out_of_range"错误
- 试图创建流或fopen时程序崩溃
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 如何找出应用程序崩溃的原因 - Win 10 LTSB
- 操纵安卓相机的深度图导致应用程序崩溃
- 为什么当我尝试搜索双链表中第一个数据条目之外的数据时,程序崩溃了?
- DLL Made with CMake 使程序崩溃
- 程序崩溃使用boost::asio
- 调用 free() 有时会导致程序崩溃
- 邮件加密程序崩溃
- 调用 java 的回调() 时应用程序崩溃.由于 detatchThread 而获得运行时错误
- 比较迭代器会使程序崩溃,而不会在自定义气泡排序实现中出现错误
- For 循环在尝试读取数组 c++ 时程序崩溃
- 即使有 0 个错误,Getter 似乎也会使程序崩溃
- Windows桌面程序保存您的计算机会话 - 基于程序崩溃时的恢复会话
- 无法访问的代码如何导致我的程序崩溃?
- 矢量迭代器在尝试调用函数时使我的程序崩溃
- QCompleter set模型使应用程序崩溃
- 使用唯一指针调用函数会使我的程序崩溃
- 删除SDL_PollEvent时程序崩溃