What to choose as a good marker in C? [closed]
So, I'm wondering if there's a good marker to choose in C, apart from the common 0xDEADBEEF
or the less appealing (for proper code) 0x0BADA550
.
What's your favorite? Is there a开发者_StackOverflowny reason to choose one particular value or another?
Wikipedia has a whole page on this subject; it provides a lot of examples and famous software in which they are used.
Anyway, if you are working on x86 you should consider following @torak's suggestion, memory filled with int 3
saved me several times. If you feel creative, you may make it more recognizable and use CC90CC90
, which translates to alternated int 3
and nop
.
The only marker that I can think of that might have more than asthetic value is 0xCCCCCCCC
. If, through some kind of error, it was executed 0xCC
translates to an INT 3
instruction.
This will work in IA-32 and x86-64. Im not sure if there are equivalents for other architectures.
Well I always picked 0x80000000
, 0x80000001
incrementing for each new region type to tickle unexpected values with signed integers. Use of these values will be strangely large for unsigned types, largely negative for signed types, and abruptly become positive for any subtractions made (thereby testing for other bugs at the same time). This has another neat side effect, in that various ALU bits will be set such as overflow, which can be detected through use of -ftrapv
and other compiler flags.
0xBABECAFE
, 0xBADADD00
, 0xBADBAD00
, 0xFADEFADE
0xCAFEBABE
is, I understand, the magic number for Java. The only thing that makes one better than another is how unlikely it is to appear in data or uninitialized memory. So don't use 0x00000000
or all F
s.
It depends what you're trying to mark. For example, do you need to distinguish between allocated but uninitialized memory and deallocated memory? If so, you need different markers for the two.
Everyone has their preference. If everyone on the team uses a different marker, it can help to determine the source of an error. As in:
"DEADBEEF? Oh, this must be from John's code."
精彩评论