In Visual Studio, we've all had "baadf00d", have seen seen "CC" and "CD" when inspecting variables in the debugger in C++ during run-time.
From what I understand, "CC" is in DEBUG mode only to indicate when a memory has been new() or alloc() and unitilialized. While "CD" represents delete'd or free'd memory. I've only seen "baadf00d" in RELEASE build (but I may be wrong).
Once in a while, we get into a situation of tacking memory leaks, buffer overflows, etc and these kind of information comes in handy.
Would somebody be kind enough to point out when and in what modes the memory are set to recognizable byte patterns for debugging purpose?
Regarding
0xCC
and0xCD
in particular, these are relics from the Intel 8088/8086 processor instruction set back in the 1980s.0xCC
is a special case of the software interrupt opcodeINT
0xCD
. The special single-byte version0xCC
allows a program to generate interrupt 3.Although software interrupt numbers are, in principle, arbitrary,
INT 3
was traditionally used for the debugger break or breakpoint function, a convention which remains to this day. Whenever a debugger is launched, it installs an interrupt handler forINT 3
such that when that opcode is executed the debugger will be triggered. Typically it will pause the currently running programming and show an interactive prompt.Normally, the x86
INT
opcode is two bytes:0xCD
followed by the desired interrupt number from 0-255. Now although you could issue0xCD 0x03
forINT 3
, Intel decided to add a special version--0xCC
with no additional byte--because an opcode must be only one byte in order to function as a reliable 'fill byte' for unused memory.The point here is to allow for graceful recovery if the processor mistakenly jumps into memory that does not contain any intended instructions. Multi-byte instructions aren't suited this purpose since an erroneous jump could land at any possible byte offset where it would have to continue with a properly formed instruction stream.
Obviously, one-byte opcodes work trivially for this, but there can also be quirky exceptions: for example, considering the fill sequence
0xCDCDCDCD
(also mentioned on this page), we can see that it's fairly reliable since no matter where the instruction pointer lands (except perhaps the last filled byte), the CPU can resume executing a valid two-byte x86 instructionCD CD
, in this case for generating software interrupt 205 (0xCD).Weirder still, whereas
CD CC CD CC
is 100% interpretable--giving eitherINT 3
orINT 204
--the sequenceCC CD CC CD
is less reliable, only 75% as shown, but generally 99.99% when repeated as an int-sized memory filler.Macro Assembler Reference, 1987
This link has more information:
http://en.wikipedia.org/wiki/Magic_number_(programming)
There's actually quite a bit of useful information added to debug allocations. This table is more complete:
http://www.nobugs.org/developer/win32/debug_crt_heap.html#table