在程序中增加相应的内存检测工具?
#define CRTDBG MAP ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#ifdef DEBUG
#ifndef DBGNEW
#define DBG_NEW new (_NORMAL_BLOCK,_FILE_LINE_)
#define new DBG NEW
#endif
#endif
_CrtDumpMemoryLeaks();
当没有释放内存时候:
#define _CRT_SECURE_NO_WARNINGS
#define CRTDBG MAP ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#include<iostream>
#include<stdio.h>
#include<Windows.h>
#ifdef DEBUG
#ifndef DBGNEW
#define DBG_NEW new (_NORMAL_BLOCK,_FILE_LINE_)
#define new DBG NEW
#endif
#endif
using namespace std;
void A_live() {
int* p = new int[1024];
//挥霍
p[0] = 0;
//申请的内存必须释放
//delete[] p;
}
int main() {
for (int i = 0; i < 5; i++)
{
A_live();
Sleep(50);
_CrtDumpMemoryLeaks();
system("pause");
return 0;
}
}
增加了delete时候:
#define _CRT_SECURE_NO_WARNINGS
#define CRTDBG MAP ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#include<iostream>
#include<stdio.h>
#include<Windows.h>
#ifdef DEBUG
#ifndef DBGNEW
#define DBG_NEW new (_NORMAL_BLOCK,_FILE_LINE_)
#define new DBG NEW
#endif
#endif
using namespace std;
void A_live() {
int* p = new int[1024];
//挥霍
p[0] = 0;
//申请的内存必须释放
delete[] p;
}
int main() {
for (int i = 0; i < 5; i++)
{
A_live();
Sleep(50);
_CrtDumpMemoryLeaks();
system("pause");
return 0;
}
}
?