??????????壬_CrtMemDifference ??????????????s1 ?? s2???????????????????????????s3???? ???????????β???? _CrtMemCheckpoint ??????????_CrtMemDifference ??????????????й????????????? ??????й??????????? _CrtMemCheckpoint ????????????????????????????????λй???
?????????????????????????????????λ??е????GetMemory??????
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#include <iostream>
using namespace std;
_CrtMemState s1?? s2?? s3;
void GetMemory(char *p?? int num)
{
p = (char*)malloc(sizeof(char) * num);
}
int main(int argc??char** argv)
{
_CrtMemCheckpoint( &s1 );
char *str = NULL;
GetMemory(str?? 100);
_CrtMemCheckpoint( &s2 );
if ( _CrtMemDifference( &s3?? &s1?? &s2) )
_CrtMemDumpStatistics( &s3 );
cout<<"Memory leak test!"<<endl;
_CrtDumpMemoryLeaks();
return 0;
}
??????????????????????????

???????????s1??s2?????????й??????????GetMemory??????s1??s2????????????????????????
????3??Linux???μ????й????
????????????????????vs?????????“????crtdbg.h???? malloc ?? free ???????????????汾???? _malloc_dbg ?? _free_dbg????????????????????????????? ???????????汾???????ж?????_DEBUG???з????? ?????汾???????? malloc ?? free ??????”???malloc??free?????????????????????????
????Linux??????????????????——mtrace??http://en.wikipedia.org/wiki/Mtrace??????????????????????????μ????????????????????????????????????valgrind????????????

?????????????????
????==6118== 100 bytes in 1 blocks are definitely lost in loss record 1 of 1
????==6118==    at 0x4024F20: malloc (vg_replace_malloc.c:236)
????==6118==    by 0x8048724: GetMemory(char*?? int) (in /home/netsky/workspace/a.out)
????==6118==    by 0x804874E: main (in /home/netsky/workspace/a.out)
????????main?е?????GetMemory???μ????й???GetMemory?????????malloc????й???100??????檔
????Things to notice:
????? There is a lot of information in each error message; read it carefully.
????? The 6118 is the process ID; it’s usually unimportant.
????? The ?rst line ("Heap Summary") tells you what kind of error it is.
????? Below the ?rst line is a stack trace telling you where the problem occurred. Stack traces can get quite large?? and be
????confusing?? especially if you are using the C++ STL. Reading them from the bottom up can help.
????? The code addresses (eg. 0x4024F20) are usually unimportant?? but occasionally crucial for tracking down weirder
????bugs.
????The stack trace tells you where the leaked memory was allocated. Memcheck cannot tell you why the memory leaked??
????unfortunately. (Ignore the "vg_replace_malloc.c"?? that’s an implementation detail.)
????There are several kinds of leaks; the two most important categories are:
????? "de?nitely lost": your program is leaking memory -- ?x it!
????? "probably lost": your program is leaking memory?? unless you’re doing funny things with pointers (such as moving
????them to point to the middle of a heap block)
????Valgrind???????????http://valgrind.org/docs/manual/manual.html??
????4?????
??????????й?????????????????????malloc/new??????????????????????????free/delete????????malloc/new??free/delete????????????????????????????????malloc???ж????free?????new???ж????deleted???????????????????????????
????????????й????????????????
?????????????????????????malloc/new??free/delete???
??????????й??????????????malloc/new??free/delete???????Щ???????????????????????????ú??????????????????????п???????????????????????????