Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the not allocated memory is marked like 0xCC? [duplicate]

Why is memory I haven't initialized set to 0xCC?

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

like image 796
Sergey Vystoropskyi Avatar asked Aug 08 '12 12:08

Sergey Vystoropskyi


Video Answer


1 Answers

Inside CRT: Debug Heap Management

When you compile a debug build of your program with Visual Studio and run it in debugger, you can see that the memory allocated 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).

Magic Number on Wiki:

CCCCCCCC Used by Microsoft's C++ debugging runtime library to mark uninitialised stack memory

In Visual Studio CRT Source, \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))
like image 121
Roman R. Avatar answered Sep 19 '22 16:09

Roman R.