c++ 内存泄露的检查

对于c++的内存泄露检测,除了我们自己手动检查以外,还可以使用c++中的函数来帮助我们检测,

如下代码:

#include "stdafx.h"
#include <string>
#include<iostream>
#include <crtdbg.h>
using namespace std;
int main()
{

    char *p=new char[10];
    //char *pp=new char[100];
    
    delete p;
    _CrtDumpMemoryLeaks();
    //cout<<p<<endl;
    return 0;
}

调试的时候按F5,而不是ctrl+F5否则你看不到任何调试信息。

如果将上面代码中的注释取消掉,那么会有如下的调试信息输出,提示如下:

image

调试信息会输出内存泄露的大小等信息。

PS:使用这个函数的时候记得加上头文件#include <crtdbg.h>。

posted @ 2015-10-14 17:44  WQZ321123  阅读(183)  评论(0编辑  收藏  举报