VisualStudio C++内存泄漏的检测方法(二)
生活随笔
收集整理的這篇文章主要介紹了
VisualStudio C++内存泄漏的检测方法(二)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
代碼
#define _CRTDBG_MAP_ALLOC #include <iostream> #include <crtdbg.h> using namespace std; void test1() {int* p = new int[10];//int* p = (int*)malloc(sizeof(int) * 10); } void test2() {int *p = new int[20];//int* p = (int*)malloc(sizeof(int) * 20); } int main() {test1();test2();_CrtDumpMemoryLeaks();return 0; }crtdbg頭文件
添加頭文件#include <crtdbg.h>
在程序退出前,也就是main函數(shù)return前,加上_CrtDumpMemoryLeaks()函數(shù)
顯示結(jié)果1,使用new,使用#define _CRTDBG_MAP_ALLOC與否都一樣
顯示結(jié)果2,使用malloc,不使用#define _CRTDBG_MAP_ALLOC
顯示結(jié)果3,使用malloc,使用#define _CRTDBG_MAP_ALLOC,會顯示內(nèi)存泄漏出在哪一行代碼
自帶快照功能
Linux下可以使用valgrind工具
總結(jié)
以上是生活随笔為你收集整理的VisualStudio C++内存泄漏的检测方法(二)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux搭建web服务器原理,【LIN
- 下一篇: MySQL MVCC 概述