首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何编写析构函数来编译代码并释放所有已分配的内存?

如何编写析构函数来编译代码并释放所有已分配的内存?
EN

Stack Overflow用户
提问于 2013-04-08 03:19:35
回答 2查看 120关注 0票数 0

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

代码语言:javascript
复制
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;当我想重复第一个time...but时,它工作得很好,但是它崩溃了

代码语言:javascript
复制
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;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-04-08 03:22:39

像这样的东西?

代码语言:javascript
复制
~CMatrix() {
    delete[] _arrayofarrays;
}

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

代码语言:javascript
复制
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

    // ...
}

更新:

(来自评论)我不能使用这个代码...分段错误实际上是2D数组,因此这会导致程序fails..probably _arrayofarrays运行

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

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

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

编辑:

operator -的代码中,您正在执行以下操作:

代码语言:javascript
复制
l._arrayofarrays = this->_arrayofarrays;

这样,您就拥有了两个封装同一数组的矩阵对象:因此,删除其中一个将使另一个也无效。这可能是问题的根本原因。

此外,您在其中创建了太多的临时文件。除非我遗漏了什么,否则这就足够了:

代码语言:javascript
复制
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;
}
票数 4
EN

Stack Overflow用户

发布于 2013-04-08 03:31:21

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

代码语言:javascript
复制
    delete [] _arrayofarrays;

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

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

代码语言:javascript
复制
CMatrix(const CMatrix& other) : _arrayofarrays(), x(other.x), y(other.y) {
    delete [] _arrayofarrays;

这是没用的,还没有要删除的东西。

代码语言:javascript
复制
    _arrayofarrays = new double[x * y];
    if (_arrayofarrays)

这个if是没有用的,new要么抛出一个异常,要么返回一个非空指针,所以检查它是否为空是没有用的。

代码语言:javascript
复制
        std::copy(other._arrayofarrays, other._arrayofarrays + (x * y), _arrayofarrays);
}

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

代码语言:javascript
复制
CMatrix& operator =(const CMatrix& rval) {
    delete [] _arrayofarrays;
    _arrayofarrays = new double[ rval.x * rval.y];

如果new分配抛出异常,那么您让_arrayofarrays保持悬空指针,您应该在删除旧内存之前分配新内存,或者在使用delete[]之后将旧指针置零

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15866723

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档