Dereferencing a null pointer

前端 未结 5 2049
时光说笑
时光说笑 2021-01-18 05:10

Why I can\'t dereference a null pointer? That is, why I can\'t read/write memory which address is simply 0?

Does the base pointer of my process have a different addr

5条回答
  •  别那么骄傲
    2021-01-18 05:32

    C 2011 online draft

    6.3.2.3 Pointers
    ...
    3 An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant. 66) If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.

    66) The macro NULL is defined in (and other headers) as a null pointer constant; see 7.19.

    Emphasis mine. NULL is defined to be an invalid pointer value that represents a well-defined "nowhere". You can't dereference it because there's nothing to dereference. Note that although the null pointer constant is always 0-valued, the null pointer value doesn't have to be; it can be 0x00000000 or 0xDEADBEEF or something completely different; that's up to the platform.

    TL;DR, NULL doesn't represent address 0; it represents "no address".

提交回复
热议问题