vendredi 27 février 2015

Are NULL and 0 completely equivalent in C?



Are there "hard" reasons for using NULL in preference to 0 in C89/C99+, and interchanging them without a second thought as to deep concerns relating to standards compliance, even in code using very obscure aspects of C.


I am concerned with "hard" things like standards compliance, portability, undefined behviour, unexpectedly different interactions with unanticipated corners of the language, whether the (hypothetical) Milliard Gargantubrain Segmented Memory Supercomputer would likely release its magic smoke, etc, should you substitute one for the other.


There is already a similar question on this site about C++, but I am not concerned about C++. I believe this is one of the areas where C++ behaviour might likely differ from C.


The issues of style and intent and, while important, don't think that it is on-topic or useful to discuss them in a Q&A forum.


The answers which I and others have found refer primarily to testing and assignment (which we commonly see, and for which it is safe to use interchangably). I'm asking if there is any thinking required at all to take arbitrarily weird but standards-compliant code which you have found, and syntactically substitute NULL for 0 (or possibly vice versa when the 0 is a pointer), whether or not that is a wise thing to do stylistically. It is hard to give exmaples of unanticipated interactions but, for exmaple, funciton pointers often catch us out, maybe sizeof, ....


I've read the relevant standards sections which describe how 0 and NULL behave, could someone help me with the impact of that on such interchange of pointer constant 0 and NULL in obscure cases? Or assure me that there are none.




Aucun commentaire:

Enregistrer un commentaire