#include <stdio.h> #include <stdlib.h> #include <string.h> #ifdef _DEBUG #define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__) #else #define DEBUG_CLIENTBLOCK #endif // _DEBUG #define _CRTDBG_MAP_ALLOC #include <stdlib.h> #include <crtdbg.h> #ifdef _DEBUG #define new DEBUG_CLIENTBLOCK #endif // _DEBUG int main() { int *a = NULL; a = (int *)malloc(100*sizeof(int)); printf("hello"); /** 寫在 運行結果前 : 在 不啓用 free() 函數前, 不 添加任何斷點, 啓用F5進行debug調試模式, 在vs2013 編輯器的調式/輸出中能夠看到以下結果. Detected memory leaks! Dumping objects -> d:\documents\visual studio 2012\projects\mem_leaks\main.cpp(22) : {66} normal block at 0x003A9D38, 400 bytes long. Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD Object dump complete. 說明有內存泄漏了, **/ //free(a); _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF|_CRTDBG_LEAK_CHECK_DF); return 0; }