如何编写析构函数才能编译代码并释放所有分配的内存



我已经创建了自定义类,它的行为应该像一个矩阵。我做了一些基本的操作,一切似乎都很顺利……然而,我不知道,我应该写什么到这个类的析构函数来释放所有已分配的内存。你能给我点建议吗?

class CMatrix {
public:
class Proxy {
    friend class CMatrix;
    const CMatrix* cmat;
    CMatrix *matrix;
    size_t n;
    Proxy(const CMatrix& m, size_t i)
    : cmat(&m), matrix(), n(i) {
    }
    Proxy(CMatrix& m, size_t i)
    : cmat(&m), matrix(&m), n(i) {
    }
    public:
    const double& operator[](size_t j) const {
       return cmat->_arrayofarrays[n * cmat->y + j];
    }
    double& operator[](size_t j) {
        if (matrix) {
            return matrix->_arrayofarrays[n * cmat->y + j];
        } else return cmat->_arrayofarrays[cmat->y];
    }
    };
    const Proxy operator[](size_t i) const {
        return Proxy(*this, i);
    }
    Proxy operator[](size_t i) {
        return Proxy(*this, i);
    }
    CMatrix() {
    _arrayofarrays = NULL;
    x = 0;
    y = 0;
};
// constructor
CMatrix(size_t x, size_t y) : _arrayofarrays(), x(x), y(y) {
    _arrayofarrays = new double[ x * y ]();
}
// destructor
~CMatrix() {
    // ?!?!???!?!?!?!!!!?!?!? 
    // #$#%@^$!!!!@$#%!!
}
// copy constructor
CMatrix(const CMatrix& other) : _arrayofarrays(), x(other.x), y(other.y) {
    delete [] _arrayofarrays;
    _arrayofarrays = new double[x * y];
    if (_arrayofarrays)
        std::copy(other._arrayofarrays, other._arrayofarrays + (x * y), _arrayofarrays);
}
CMatrix& operator =(const CMatrix& rval) {
    delete [] _arrayofarrays;
    _arrayofarrays = new double[ rval.x * rval.y];
    std::copy(rval._arrayofarrays, rval._arrayofarrays + (rval.x * rval.y), _arrayofarrays);
    x = rval.x;
    y = rval.y;
    return *this;
}

double *_arrayofarrays;
size_t x;
size_t y;
};
编辑:

实际上现在我已经意识到,它在运行这部分代码后崩溃了。在调用这段代码之前,我有一个类的实例,我们叫它a,b,c然后我想设置a = b-c;这是第一次效果很好……但是当我想重复它的时候,它就崩溃了

CMatrix CMatrix::operator-(const CMatrix &matrix) const {
if (this->x != matrix.x || this->y != matrix.y) {
    throw CSizeException(matrix.y, matrix.x, this->y, this->x, '+');
};
CMatrix m(this->x, this->y);
CMatrix l(matrix.x, matrix.y);
l._arrayofarrays = this->_arrayofarrays;
CMatrix o(matrix.y, matrix.y);
o = matrix;

CMatrix result(this->x, this->y);
for (unsigned int i = 0; i < this->x; i++)
    for (unsigned int j = 0; j < this->y; j++)
        m[i][j] = l[i][j] - o[i][j];           
return m;
}

像这样?

~CMatrix() {
    delete[] _arrayofarrays;
}

所有用new[]分配的数组必须通过对delete[]的匹配调用来销毁。此外,还可以从复制构造函数中删除delete[]语句:

CMatrix(const CMatrix& other) : _arrayofarrays(), x(other.x), y(other.y) {
    // delete [] _arrayofarrays;
    // ^^^^^^^^^^^^^^^^^^^^^^^^^
    // This is unnecessary: you are creating a new object, so this data member
    // is not pointing to any previously allocated array
    // ...
}

更新:

(来自注释)我不能使用这段代码…_arrayofarrays实际上是2D数组,所以这导致程序运行失败…可能是segfault

这是不正确的:_arrayofarrays是一个1D数组,调用delete[]是销毁它的正确方法。如果这样做会导致段错误,那么可能是您在其他代码中做错了什么。

作为建议,避免通过原始指针、newdelete(或其对应的数组)进行手动内存管理通常是一个好主意,因为它容易出错,并且容易导致内存泄漏或无效指针/引用的解引用。

考虑使用标准容器,如std::vector<>std::deque<>


编辑:

operator -的代码中你正在做:

l._arrayofarrays = this->_arrayofarrays;
这样你就有了两个矩阵对象,它们封装了同一个数组:因此,删除其中一个将使另一个也无效。这可能是你的问题的根本原因。

还有,你在里面创建了太多的临时。除非我遗漏了什么,否则这应该足够了:

CMatrix CMatrix::operator-(const CMatrix &matrix) const {
    if (this->x != matrix.x || this->y != matrix.y) {
        throw CSizeException(matrix.y, matrix.x, this->y, this->x, '+');
    };
    CMatrix result(this->x, this->y);
    for (unsigned int i = 0; i < this->x; i++)
        for (unsigned int j = 0; j < this->y; j++)
            result[i][j] = (*this)[i][j] - matrix[i][j];
    return result;
}

在析构函数中删除内存的方法与在其他成员函数中删除内存的方法相同:

    delete [] _arrayofarrays;

如果这个程序崩溃了,那么你一定是在其他地方破坏了内存。尝试使用valgrind来检查内存错误。

复制构造函数有一些问题:

CMatrix(const CMatrix& other) : _arrayofarrays(), x(other.x), y(other.y) {
    delete [] _arrayofarrays;

这是无用的,没有什么要删除的

    _arrayofarrays = new double[x * y];
    if (_arrayofarrays)

这个if是无用的,new会抛出一个异常或返回一个非空指针,所以检查它是否为空是无用的。

        std::copy(other._arrayofarrays, other._arrayofarrays + (x * y), _arrayofarrays);
}

你的赋值操作符也有一些问题:

CMatrix& operator =(const CMatrix& rval) {
    delete [] _arrayofarrays;
    _arrayofarrays = new double[ rval.x * rval.y];

如果new分配抛出一个异常,那么你让_arrayofarrays持有一个悬空指针,你应该在删除旧内存之前分配新内存,或者在使用delete[]

后将旧指针归零。

最新更新