本文源自:http://m.shnenglu.com/kerlw/services/trackbacks/21700.aspx
首先說明一下什么是Heap Corruption。當輸入超出了預分配的空間大小,就會覆蓋該空間之后的一段存儲區域,這就叫Heap Corruption。這通常也被用作黑客攻擊的一種手段,因為如果在該空間之后的那段存儲區域如果是比較重要的數據,就可以利用Heap Corruption來把這些數據修改掉了,后果當然可想而知了。
在VC里面,用release模式編譯運行程序的時候,堆分配(Heap allocation)的時候調用的是malloc,如果你要分配10byte的空間,那么就會只分配10byte空間,而用debug模式的時候,堆分配調用的是_malloc_dbg,如果你只要分配10byte的空間,那么它會分配出除了你要的10byte之外,還要多出約36byte空間,用于存儲一些薄記信息,debug堆分配出來之后就會按順序連成一個鏈。
那么我們再來看看薄記信息中有些什么。還是上面10byte分配空間的例子,那么分配出的10byte空間的前面會有一個32byte的附加信息,存儲的是一個_CrtMemBlockHeader結構,可以在DBGINT.H中找到該結構的定義:
typedef struct _CrtMemBlockHeader
{
// Pointer to the block allocated just before this one:
struct _CrtMemBlockHeader *pBlockHeaderNext;
// Pointer to the block allocated just after this one:
struct _CrtMemBlockHeader *pBlockHeaderPrev;
char *szFileName; // File name
int nLine; // Line number
size_t nDataSize; // Size of user block
int nBlockUse; // Type of block
long lRequest; // Allocation number
// Buffer just before (lower than) the user's memory:
unsigned char gap[nNoMansLandSize];
} _CrtMemBlockHeader;
/* In an actual memory block in the debug heap,
* this structure is followed by:
* unsigned char data[nDataSize];
* unsigned char anotherGap[nNoMansLandSize];
*/


