1. 程式人生 > >為什麼VC經常輸出燙燙燙燙燙燙燙燙(記憶體在不同狀態下預設填充的字元)

為什麼VC經常輸出燙燙燙燙燙燙燙燙(記憶體在不同狀態下預設填充的字元)

在Debug 模式下,
VC 會把未初始化的棧記憶體全部填成0xcc,當字串看就是 燙燙燙燙……
會把未初始化的堆記憶體全部填成0xcd,當字串看就是 屯屯屯屯……
可以讓我們方便地看出那些記憶體沒初始化

但是Release 模式下不會有這種附加動作,原來那塊記憶體裡是什麼就是什麼

名字      描述
0xCD   Clean Memory    申請的記憶體由malloc或者new完成
0xDD   Dead Memory    釋放後的記憶體,用來檢測懸垂指標
0xFD   Fence Memory    動態申請後的記憶體值,沒有初始化。用來檢測陣列的下標界限
0xAB   (Allocated Block?)    使用LocalAlloc()分配的記憶體 0x0DF0ADBA Bad Food     使用LocalAlloc並且引數為LMEM_FIXED,但是還沒寫入
0xCC

    使用了/GZ選項,沒有初始化的自動變數在DBGHEAP.C檔案中,

Microsoft's memorymanagement functions often initialize memory with special values. The following article describes frequent used variants. 
Microsoft Visual C++ Runtime library, C runtime library provides it own debug codes:

0xCD, 0xCDCDCDCD - New objects. New objects are filled with 0xCD when they areallocated.
0xFD

, 0xFDFDFDFD - No-man's land memory. Extra bytes that belong to the internal block allocated, but not the block you requested. They are placedbefore and after requested blocks and used for data bound checking.
0xDD, 0xDDDDDDDD - Freed blocks. The freed blocks kept unused in the debugheap's linked list when the _CRTDBG_DELAY_FREE_MEM_DF flag is set are currentlyfilled with 0xDD. Although in some cases you won't see magic 0xDDDDDDDD value,as it will be overwritten by another debug function (e.g. 0xFEEEFEEE forHeapFree).

These constants are defined in DbgHeap.c file as:
1. static unsigned char _bNoMansLandFill = 0xFD; /* fill no-man's land with this*/
2. static unsigned char _bDeadLandFill = 0xDD; /* fill free objects with this */
3. static unsigned char _bCleanLandFill = 0xCD; /* fill new objects with this */

Compiler initialisations
0xCC
, 0xCCCCCCCC - The /GX Microsoft Visual C++ compiler option initialises alllocal variables not explicitly initialised by the program. It fills all memoryused by these variables with 0xCC, 0xCCCCCCCC. 

Windows NT memory codes
0xABABABAB - Memory following a block allocated by LocalAlloc(). 
0xBAADF00D - "Bad Food". This is memory allocated via LocalAlloc(LMEM_FIXED, ... ). It is memory that has been allocated but not yet written to.
0xFEEEFEEE - OS fill heap memory, which was marked for usage, but wasn'tallocated by HeapAlloc() or LocalAlloc(). Or that memory just has been freed byHeapFree().