基类与派生类的析构函数(1)
2016-01-03 21:12
225 查看
#define _CRTDBG_MAP_ALLOC #include <iostream> #include <stdlib.h> #include <crtdbg.h> using namespace std; #ifdef _DEBUG #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__) #define new DEBUG_NEW #endif class parent { public: parent() { cout << "parent new" << endl; p = new char(100); } ~parent() { cout << "parent delete" << endl; delete p; } private: void *p; }; class son : public parent{ public: son() { q = new char(100); cout << "son new" << endl; } ~son() { cout << "son delete" << endl; delete q; } private: void *q; }; void t1() { /* parent new son new son delete parent delete */ son* s = new son(); delete s; } void t2() { /* parent new son new parent delete error: memory leak */ parent * p = new son(); delete p; } int _tmain(int argc, _TCHAR* argv[]) { // dump memory leak information _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); <span style="white-space:pre"> </span>// 存在内存泄露 t2(); return 0; }
相关文章推荐
- linux禁止root用户直接登录sshd并修改默认端口
- HDU 1394 Minimum Inversion Number(循环数组求逆序数)(暴力,归并排序,线段树)
- linux中which whereis locate find 的用法
- terminal
- 修改主机名
- centos系统下pip安装使用详解
- 从抑郁中走出来
- 别人对CSAPP的阅读理解
- iOS 疑难杂症(什么鬼系列)— — 复制 Storyborad 莫名崩溃
- 直接插入排序(Straight Insertion Sort)的C语言实现
- hibernate spring annotation setup
- MySQL Cluster(MySQL 集群) 初试(转)
- List、Set、Map的遍历方法
- 【bzoj4278】 [ONTAK2015]Tasowanie 后缀数组
- 第六届河南省赛 zzulioj 1487: Adjacent Bit Counts (01串DP)nyoj 715
- MySQL数据库的优化–运维架构师必会高薪技能,笔者近六年来一线城市工作实战经验
- 备忘录模式
- C语言内存管理:malloc、calloc、free的实现
- 2016元旦上课 ocp
- 2-3