c++中delete 和 delete []简单比较

测试代码:

//: C07:TestDelete.cpp
// compare the delete and delete[]
#include<iostream>
using namespace std;

class Test {
    public: Test() {
        cout << "Construct the class Test" << endl;
    }
    public: ~Test() {
        cout << "Destruct the class Test" << endl;
    }
};

int main() {
    cout << "Test delete:" << endl;
    Test* p = new Test[3];
    delete p;    // delete will only call destruct function for first object
    cout << "Test delet[]:" <<endl;
    Test* p1 = new Test[3];
    delete[] p;    // delete[] will call the destruct function for each object
}

测试结果:

c++中delete 和 delete []简单比较

总结:delete 回收内存时,仅调用第一个对象的析构函数;

         delete []会调用所有对象的析构函数。

参考文献:http://jazka.blog.51cto.com/809003/230220;http://blog.csdn.net/xwdok/article/details/1416790

c++中delete 和 delete []简单比较,布布扣,bubuko.com

c++中delete 和 delete []简单比较

上一篇:java中的MessageDigest类


下一篇:Django学习笔记2-Python IDE