2024年4月29日发(作者:)

为什么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 memory management functions often initialize memory with s

pecial 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 wh

en they are allocated.

0xFD, 0xFDFDFDFD - No-man's land memory. Extra bytes that belong to

the internal block allocated, but not the block you requested. They are place

d before and after requested blocks and used for data bound checking.

0xDD, 0xDDDDDDDD - Freed blocks. The freed blocks kept unused in th

e debug heap's linked list when the _CRTDBG_DELAY_FREE_MEM_DF flag is se

t are currently filled with 0xDD. Although in some cases you won't see magi

c 0xDDDDDDDD value, as it will be overwritten by another debug function

(e.g. 0xFEEEFEEE for HeapFree).

These constants are defined in DbgHeap.c file as

static unsigned char _bNoMansLandFill = 0xFD; /* fill no-man's land with

this */

static unsigned char _bDeadLandFill = 0xDD; /* fill free objects with this

*/

static unsigned char _bCleanLandFill = 0xCD; /* fill new objects with this

*/

Compiler initialisations

0xCC, 0xCCCCCCCC - The /GX Microsoft Visual C++ compiler option initia

lises all local variables not explicitly initialised by the program. It fills all me

mory used 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( LME

M_FIXED, ... ). It is memory that has been allocated but not yet written to.

0xFEEEFEEE - OS fill heap memory, which was marked for usage, but was

n't allocated by HeapAlloc() or LocalAlloc(). Or that memory just has been fr

eed by HeapFree().