delete

delete与析构函数

gcc下调用两次析构函数的问题

#include <iostream> 
#include <stdlib.h>
using namespace std;

class A
{
public:
    A() { cout << "A\n"; }
    A(const A& b) { cout << "copy A\n"; }
    ~A() { cout << "~A\n"; }
};

int main()
{
    // A b;
    // delete &b;//gcc 调用两次析构 VS报错
    return 0;
}

**原代码中的A是建立在栈中
由系统自动释放内存
而delete是为动态分配内存而设计的,析构的是堆内存
所以gcc会有两次析构(没有报错也是很奇怪了)
在VS下由于delete没有堆内存所以VS下运行出错**

猜你喜欢

转载自blog.csdn.net/qq_40953281/article/details/80914679
今日推荐