摘要: 回答此问题时,照下面顺序回答: 1、malloc free是库函数,new delete是运算符。 2、malloc free只是申请/释放内存,不能构造和析构对象;new free可以申请/释放内存,构造/析构对象。 3、举例说明第2点: #include <iostream>#include <stdlib.h>using namespace std;class Test{public: ... 阅读全文
posted @ 2013-05-07 11:12 helloweworld 阅读(214) 评论(0) 推荐(0) 编辑
摘要: 是什么 一个类模板,解决delete发生之前发生异常从而导致内存泄露的问题。 使用时需要包含memory头文件。 void f(){ int *ip = new int(42); ...//如果这里发生异常,则下面的delete可能不会执行,导致内存泄露。 delete ip;}#include <iostream>#include <memory>using namespace std;vo... 阅读全文
posted @ 2013-05-07 10:08 helloweworld 阅读(174) 评论(0) 推荐(0) 编辑