0xDEADBEEF equivalent for 64-bit development?

前端 未结 12 578
栀梦
栀梦 2021-02-02 06:46

For C++ development for 32-bit systems (be it Linux, Mac OS or Windows, PowerPC or x86) I have initialised pointers that would otherwise be undefined (e.g. they can not immedi

相关标签:
12条回答
  • 2021-02-02 07:01

    Two 0xDEADBEEFs should be enough, I think..

    0 讨论(0)
  • 2021-02-02 07:01

    I see several answers claiming NULL is a good choice, but I disagree.

    NULL is often used as a valid return value from functions. It indicates a failure return or an unknown value. This is a different meaning than "uninitialized pointer."

    Using a debugger on the code and seeing NULL would then leave two possibilities: the pointer was never initialized or it had failed a memory allocation.

    Setting the uninitialized pointer to 0xDEADBEEF or the 64-bit equivalent means that a NULL pointer indicates an intentional value.

    0 讨论(0)
  • 2021-02-02 07:02

    Most current 64-bit systems let you use only the lowest 248–252 bits of the address space; higher bits of the address must be all-zero. Some chips (e.g. amd64) also let you use the highest 248–252. Addresses outside these ranges cannot ever be mapped to accessible memory; the hardware simply won't allow it.

    I therefore recommend you use a value close to 263, which is nowhere near either of the possibly-usable spaces. If the leading four hex digits are 7ff8, the value will be a double precision floating-point NaN, which is convenient. So my suggested cute hexadecimal phrase is 0x7FF8BADFBADFBADF.

    By the way, you really don't want to use a value close to 0, because that makes it hard to tell an offset dereference of NULL — a structure member access, for instance — from a dereference of the poison pattern.

    0 讨论(0)
  • 2021-02-02 07:08

    0x42 could work on both 32bit and 64bit ? (It should still trigger a crash since it is close enough to the NULL pointer, and given that it's rather large, chances are you would not have it within a regular dereference of a structure field with the structure pointer being NULL).

    0 讨论(0)
  • 2021-02-02 07:18

    0xBADC0FFEE0DDF00D

    0 讨论(0)
  • 2021-02-02 07:18

    I don't have a good choice for you, but here's a list of hex words that you can use to make your phrase.

    0 讨论(0)
提交回复
热议问题