Why is the allocated memory marked as 0xCC?

Possible duplicate:
When and why does the OS initialize memory 0xCD, 0xDD, etc. to malloc / free / new / delete?

Why is the memory that I did not initialize not set to 0xCC ?

Setting memory to 0xCC will result in poor performance, so there must be a reason for filling this memory with this byte.

+7
source share
3 answers

Internal CRT: Debug Heap Management

When compiling the debug build of your program using Visual Studio and running it in the debugger, you will see that the allocated memory or deallocated has funny values, such as ...

0xCC When the code is compiled with the / GZ option, uninitialized variables are automatically assigned to this value (at byte level).

Wiki magic number :

CCCCCCCC Used by the Microsoft C ++ debug library to mark uninitialized stack memory

In the CRT source of Visual Studio \VC\crt\src\malloc.h :

 #define _ALLOCA_S_STACK_MARKER 0xCCCC // ... #undef _malloca #define _malloca(size) \ __pragma(warning(suppress: 6255)) \ ((((size) + _ALLOCA_S_MARKER_SIZE) <= _ALLOCA_S_THRESHOLD) ? \ _MarkAllocaS(_alloca((size) + _ALLOCA_S_MARKER_SIZE), _ALLOCA_S_STACK_MARKER) : \ _MarkAllocaS(malloc((size) + _ALLOCA_S_MARKER_SIZE), _ALLOCA_S_HEAP_MARKER)) 
+14
source

The compiler does this for you in debug mode, so if you accidentally read uninitialized memory, you will see the distinctive value 0xCC and find out that you (probably) read uninitialized memory. The value 0xCC has many other useful properties, for example, it is a machine language instruction for calling a software breakpoint if you accidentally start uninitialized memory.

The basic principle: to simplify the identification of values โ€‹โ€‹obtained by reading uninitialized memory.

This does not happen in your release builds.

This technique was introduced in writing solid code .

+5
source

When the code is compiled with the / GZ option, uninitialized variables are automatically assigned to this value (at the byte level).

0xCC - machine code instruction for calling a breakpoint. For more information, see Another Question .

+2
source

All Articles