Dereferencing a pointer to 0 in C

前端 未结 5 1319
抹茶落季
抹茶落季 2020-12-08 06:33

Sometimes data at memory address 0x0 is quite valuable -- take x86 real mode IVT as a more known example: it starts at 0x0 and contains pointers to interrupt handlers: a dwo

5条回答
  •  一整个雨季
    2020-12-08 06:45

    Annex J It is undefined behavior when...

    The operand of the unary * operator has an invalid value (6.5.3.2).

    In that same footnote you mentioned, it says a null pointer is an invalid value. Therefore, it is not prohibited, but undefined behavior. As for the distinction between address 0x0 and a null pointer, see Is memory address 0x0 usable?.

    The null pointer is not necessarily address 0x0, so potentially an architecture could choose another address to represent the null pointer and you could get 0x0 from new as a valid address.

    Whether the null pointer is reserved by the Operative System or the C++ implementation is unspecified, but plain new will never return a null pointer, whatever its address is (nothrow new is a different beast). So, to answer your question:

    Is memory address 0x0 usable?

    Maybe, it depends on the particular implementation/architecture.

    In other words, feel free to use 0x0 if you're sure on your system that it won't cause a crash.

提交回复
热议问题