使用删除正在崩溃我的程序

时间:2015-12-19 22:28:42

标签: c++

当我尝试删除关于distractor的指针时,我的程序崩溃了,为什么? 我的代码中没有理解我做错了什么。

我是否使用了新的错误?

类矩阵:

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

主:

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

感谢

2 个答案:

答案 0 :(得分:0)

1)准备一个对象不再存在的成员函数这个词是析构函数,而不是分心者。

2)如果你是运算符new的数组形式,使用非数组形式的运算符delete - 你是 - 给出了未定义的行为。请改用运算符delete的数组形式。

3)如图所示,您的代码使用了您未提供的其他功能。任何这些,如果实现不正确,可能对指针执行无效操作,因此是导致崩溃的其他原因。

4)不要使用动态内存分配(运算符new等)来处理动态分配的数组。您已证明这样做容易出错。如果您想要动态分配double数组,请使用std::vector<double>。如果您想要一个double的二维数组,请使用std::vector<std::vector<double> >。除了不易出错之外,std::vector正确地释放其内存(只要你不写一些其他代码来破坏内存)。

答案 1 :(得分:0)

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 the arraydelete []),否则您将获得未定义的行为(例如崩溃):

for (int i = 0; i < _rows; i++)
    delete[] _array[i];   // <== delete array,  not element!! 

delete[] _array;  // <== delete array,  not element!! 

注意: 每当您想要使用新/删除时,您应该问自己,它是否值得考虑使用向量代替。这很简单,因为online demo显示代码重做

编辑:pow()

的问题

在这里创建矩阵对象temp

Matrix temp(this->_rows, this->_cols);

此对象将在函数pow()的末尾自动销毁。这意味着它的析构函数将调用所有必需的delete[]

问题是在函数结束时,你手动完成析构函数的工作,所以你delete[]临时数组。然后析构函数尝试删除已删除的对象。这是未定义的行为,因此崩溃!

你所要做的就是摆脱pow()的最后3行以及它们包含的不必要的delete[]

相关问题