windows C++内存检测

作为一个C++程序员,一个简单例子更容易理解

#include "stdafx.h"
#include<windows.h>

#ifdef _DEBUG
#define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK,__FILE__,__LINE__)
#else
#define DEBUG_CLIENTBLOCK
#endif

#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#ifdef _DEBUG
#define new DEBUG_CLIENTBLOCK
#endif

using namespace std;

int _tmain(int argc,char* argv[])
{
 int *p = new int();
 _CrtDumpMemoryLeaks();
 return 0;
}

运行完成后,查看输出窗口。
-->e:program estmemleaksmemleaksmemleaks.cpp(23) : {128} client block at 0x0066AF90, subtype 0, 4 bytes long.

这就是我们想要的结果。

原文地址:https://www.cnblogs.com/weiweiqingfeng/p/3944500.html