CRT库
头文件及宏定义:
#define _CRTDBG_MAP_ALLOC
#include<stdlib.h>
#include<crtdbg.h>
函数
在需要检测的位置加上这个函数,那么就能检测目前为止没有释放的动态内存
_CrtDumpMemoryLeaks();
如:
#define _CRTDBG_MAP_ALLOC
#include<stdlib.h>
#include <crtdbg.h>
#include <iostream>
using namespace std;
int main(){
int* p = new int[10];
_CrtDumpMemoryLeaks();
return 0;
}
输出信息
Detected memory leaks!
Dumping objects ->
{90} normal block at 0x00F458E8, 40 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
Object dump complete.
可见有40个字节没有释放(10个int)