Dereferencing a pointer to 0 in C

前端 未结 5 1318
抹茶落季
抹茶落季 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:51

    The statement:

    char * x = 0;
    

    does not necessarily put 0x0 into x. It puts the defined null pointer value for the current architecture and compiler into x.

    Now, in practical terms, all compilers / processors observed in common use do end up putting 32 (or 64) 0 bits in a row in a register or storage location in response to that statement, so, so if memory address 0 is useful, then, as others have indicated, you are stuck using formally undefined behavior. However, in once upon a time there was hardware out there for which a 'null pointer' was some bit pattern that was not all zeros, and, who knows, there may be again.

提交回复
热议问题