我正在构建一个矩阵类。为此,我构建了一个双指针数组。所以我试图为此类 MATRIX 实现析构函数,但删除 [] 未按预期运行 这是代码:
Matrix::Matrix(int r, int c):rowsM(r), colsM(c)
{
matrixM = new double* [rowsM];
assert(matrixM != NULL);
for(int i=0; i < rowsM; i++){
matrixM[i] = new double[colsM];
assert(matrixM[i] != NULL);
}
sum_rowsM = new double[rowsM];
assert(sum_rowsM != NULL);
sum_colsM = new double[colsM];
assert(sum_colsM != NULL);
}
void Matrix::destroy()
{
for(int i=0; i < rowsM; i++){
delete[] matrixM[i];
assert(matrixM[i] == NULL);
}
delete[] matrixM;
assert(matrixM != NULL);
delete[] sum_rowsM;
assert(sum_rowsM == NULL);
delete[] sum_colsM;
assert(sum_colsM == NULL);
}
我尝试使用
delete
但没有成功,所以我查了一下并发现delete[]
是更好的选择,但Cygwin(我用来编译)的结果是assertion "matrixM[i] == NULL" failed: file "matrix.cpp", function: void Matrix::destroy() Aborted
这意味着删除显然失败了。请发送帮助
删除和删除不将指针设置为空