Xref: utzoo comp.lang.c++:6811 comp.std.c:2612 Path: utzoo!attcan!uunet!auspex!guy From: guy@auspex.auspex.com (Guy Harris) Newsgroups: comp.lang.c++,comp.std.c Subject: Re: references to dereferenced null pointers Message-ID: <3036@auspex.auspex.com> Date: 14 Mar 90 17:48:02 GMT References: <25EB8EE8.8462@paris.ics.uci.edu> <1990Mar12.175613.12082@utzoo.uucp> <1623@argus.UUCP> Followup-To: comp.lang.c++ Organization: Auspex Systems, Santa Clara Lines: 66 >I'm confused, is a non zero NULL pointer valid or not? The confusion stems from confusion over the meaning of "non-zero". 4.1.5: ... The macros are NULL which expands to an implementation-defined null pointer constant; ... 3.2.2.3: ... An integral constant expression with the value 0, or such an expression cast to type "void *", is called a *null pointer constant*. So if some implementation defines NULL as something other than 0 or (void *)0 or (17 - 17) or ((void *)(17 - 17)) or..., the implementation is not a valid C implementation. *However*, this does *not* mean that the implementation must *represent* a null pointer of any type as a bit string of all zero bits. >I'm not asking if it will break 90% of the programs out there that >use 0 instead of NULL. Defining NULL as something other than the values listed (or implied...) and having that be the only null pointer constant will break all of the programs out there at use 0 instead of NULL. Thus, doing so would be invalid. Representing a null pointer constant as something other than a bit string of all zero bits will not break *any* valid ANSI C program. It will break some *in*valid programs, which is why Henry described it as "unwise" even though it's completely valid. >On a 370 here I'd love to define NULL as -1 because it will cause an >immediate addressing exception if it is referenced. But, I was told that >NULL is defined as always being the value zero. If by "define NULL as -1" you mean putting in something like #define NULL -1 that would, indeed, be invalid. If you mean changing your 3*0 C implementation such that null pointers are represented by 32 1 bits, *including making sure that the statements char *p; p = 0; assigns a value of 32 1 bits to "p"* (this is a bit that confuses a lot of people who think that the "p = 0;" is obliged to assign an all-zero-bits value to "p"), then it would be valid as long as you make sure this representation convention is followed consistently; you also have to make sure that non-C code that calls C code or is called by C code obeys this convention.