#define _CRTDBG_MAP_ALLOC
#include<iostream>
#include <crtdbg.h>
using namespace std;
int main()
{
char *a = (char*)malloc(sizeof(char)*124);
memset(a,0x00,124);
char *p = a;
free(p);
int *ptr = (int*)malloc(sizeof(int)*124);
int *ptr2 = ptr;
ptr = (int*)malloc(sizeof(int)*124);
free(ptr2);
_CrtDumpMemoryLeaks();
return 0;
}
本文探讨了C++中使用malloc和free进行内存分配与释放,并通过_CrtDumpMemoryLeaks()函数检测内存泄漏的问题。重点展示了如何正确管理内存资源以避免潜在的内存泄露。
747

被折叠的 条评论
为什么被折叠?



