使用delete会使我的程序崩溃

using delete is crashing my program

本文关键字:程序 崩溃 我的 delete 使用      更新时间:2023-10-16

当我试图删除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[]