four_in_the_morning
2015-08-02 01:12
采纳率: 100%
浏览 2.2k

C++ : 请问下面这段代码为何会出现double free 的问题?

下面这段代码对象在析构的时候判断了指针是否为空再进行操作,为何还会出现double free 的问题?

 #include<iostream>
using namespace std;
class base {
public:
    base(int *a):p(a) {}
    base(int num) :p(new int(num)) {}
    base(base& mid) :p(mid.p) {
    cout << "base is constructing!\n";
    if (p != NULL) cout << "p is not NULL!\n";
}

    ~base() { 
    if (p != NULL ) {
    delete p;
    p = NULL;
}
    if (p == NULL)
    cout << "p is NULL!\n";
}
private:
    int *p;
};
int main() {
    base one(1);
    base two(one);
}

2条回答 默认 最新

相关推荐 更多相似问题