【问题标题】:C++ deleting/(recursive) Object-Destruction -issuesC++ 删除/(递归)对象破坏问题
【发布时间】:2020-05-24 13:29:16
【问题描述】:

我无法理解为什么以下代码总是抛出此异常:

AddressSanitizer:尝试双重释放

我想我没有得到新/删除过程的某些部分,但就是想不通。

提前感谢您的帮助......或者女孩;P

#include <iostream>
using namespace std;

template <typename Key, size_t N >
class TSet {
    struct Thing; using Link = Thing *;
    struct Thing{
        Key key;
        Link link{nullptr};
        ~Thing(){ if (link){ delete[] link; link=nullptr; }}
    };
    Link root{nullptr};
    size_t size;

    void    addB_Sub(Link e){ 
        if ( e->link ) { addB_Sub(e->link); }
        else e->link= new Thing[N];
    }
public:
    TSet(): root{new Thing[N]}, size{N} {}
    ~TSet(){  delete[] root; }//if (root)

    void    addB(const size_t &i){ addB_Sub(root+i); }

std::ostream &show(std::ostream &o) {
        if (!(root)) return o;
        Link peak;
        size_t count;
        for (size_t i{size}; i--; ) { cout<<'['<<i<<']';
            count=0;
            peak=(root+i);
            while (peak->link) { ++count; peak=peak->link; }
            o<<count<<", ";
        }
        cout<<"end";
        peak=nullptr;
        return o;
    }
};
template <typename Key, size_t N>
std::ostream &operator<<(ostream& o,TSet<Key, N> g){ return g.show(o); } 

int main(){
    TSet<int,5> s;
    for (int i{7}; i>0; --i) s.addB(1);
    for (int i{4}; i>0; --i) s.addB(3);
    for (int i{2}; i>0; --i) s.addB(4);
    cout<<s<<endl;
}

【问题讨论】:

  • 在析构函数中,所有if (link){ delete[] link; link=nullptr; } 可以而且应该简单地写成delete[] link;。其余部分是多余的,表明意图错误。

标签: c++ recursion destruction double-free


【解决方案1】:

您的问题部分在这里:

template <typename Key, size_t N>
std::ostream &operator<<(ostream& o, TSet<Key, N> g);

你按值接受参数,所以你做复制。

您真正的问题是您的课程不遵守 3/5/0 规则。

所以g 析构函数和s 析构函数都释放相同的资源。

【讨论】:

  • ooooh...我只复制指针而不是指向对象....我明白了,thanX 很多!! .. 3/5/0 的规则在这里真的很有意义,再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-19
  • 2010-12-03
  • 2021-05-29
  • 1970-01-01
相关资源
最近更新 更多