Redefining NULL

前端 未结 7 1516
北恋
北恋 2020-11-30 20:53

I\'m writing C code for a system where address 0x0000 is valid and contains port I/O. Therefore, any possible bugs that access a NULL pointer will remain undetected and at t

相关标签:
7条回答
  • 2020-11-30 21:46

    The standard states that an integer constant expression with value 0, or such an expression converted to the void * type, is a null pointer constant. This means that (void *)0 is always a null pointer, but given int i = 0;, (void *)i need not be.

    The C implementation consists of the compiler together with its headers. If you modify the headers to redefine NULL, but don't modify the compiler to fix static initialisations, then you have created a non-conforming implementation. It is the entire implementation taken together that has incorrect behaviour, and if you broke it, you really have no-one else to blame ;)

    You must fix more than just static initialisations, of course - given a pointer p, if (p) is equivalent to if (p != NULL), due to the above rule.

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