在 C 中选择什么作为好的标记?
因此,我想知道除了常见的 0xDEADBEEF
或不太吸引人的(对于正确的代码)0x0BADA550
之外,C 中是否有一个好的标记可供选择。
你最喜欢什么? 是否有任何理由选择一个特定值或另一个值?
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 any reason to choose one particular value or another?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
维基百科有关于此主题的整个页面;它提供了很多示例和使用它们的著名软件。
无论如何,如果你正在使用 x86,你应该考虑遵循 @torak 的建议,充满
int 3
的内存救了我好几次。如果您觉得有创意,可以让它更容易识别并使用CC90CC90
,它转换为交替的int 3
和nop
。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 useCC90CC90
, which translates to alternatedint 3
andnop
.我能想到的唯一可能具有超过审美价值的标记是
0xCCCCCCCC
。如果由于某种错误而被执行,0xCC
会转换为INT 3
指令。这适用于 IA-32 和 x86-64。我不确定其他架构是否有等效的东西。
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 executed0xCC
translates to anINT 3
instruction.This will work in IA-32 and x86-64. Im not sure if there are equivalents for other architectures.
好吧,我总是选择
0x80000000
、0x80000001
为每个新区域类型递增,以用有符号整数来触发意外值。对于无符号类型,这些值的使用会非常大,对于有符号类型来说,这些值很大程度上是负值,并且对于所做的任何减法突然变成正值(从而同时测试其他错误)。这还有另一个巧妙的副作用,即会设置各种 ALU 位,例如溢出,可以通过使用-ftrapv
和其他编译器标志来检测。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
是我理解的魔力Java 的编号。唯一比另一个更好的是它不太可能出现在数据或未初始化的内存中。因此,请勿使用0x00000000
或全部F
。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 use0x00000000
or allF
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.
每个人都有自己的偏好。如果团队中的每个人都使用不同的标记,则可以帮助确定错误的来源。如:
“DEADBEEF?哦,这一定是约翰的密码。”
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."