Xref: utzoo comp.lang.c++:6783 comp.std.c:2588 Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!tut.cis.ohio-state.edu!ucsd!ucsdhub!hp-sdd!ncr-sd!ncrcae!cs-col!almira!haug From: haug@almira.uucp (Brian R Haug) Newsgroups: comp.lang.c++,comp.std.c Subject: Re: references to dereferenced null pointers Message-ID: <1990Mar11.222634.2701@almira.uucp> Date: 11 Mar 90 22:26:34 GMT References: <51083@microsoft.UUCP> <25EB8EE8.8462@paris.ics.uci.edu> <52081@microsoft.UUCP> <25F8D2FB.10981@paris.ics.uci.edu> <1990Mar11.015305.28264@utzoo.uucp> Reply-To: haug@Columbia.NCR.COM (Brian Haug) Organization: Personal machine, Columbia, SC Lines: 58 In article <1990Mar11.015305.28264@utzoo.uucp> henry@utzoo.uucp (Henry Spencer) writes: >In article <25F8D2FB.10981@paris.ics.uci.edu> rfg@ics.uci.edu (Ronald Guilmette) writes: >>...noted the obscure restriction that a null pointer must be >>something created from "an integral constant expression with the value 0" >>(see ANSI C standard 3.2.2.3). This could be an issue for both C and >>C++, since it would seem to place in doubt the validity of the following: >> >> int nil () >> { >> return 0; >> } >> >> ... >> >> void *vp; >> >> vp = (void *) nil (); > >This code is illegal in C, and always has been. There is no general >relationship between the integer value 0 and the null pointer. An >integer *constant* value equal to zero in a pointer context is automatically >converted to a null pointer; this may be a non-trivial conversion, and is >guaranteed valid only at compile time. The result of converting the >integer value 0 to a pointer at run time is implementation-defined. Henry, I beg to differ. I see where you are coming from with the wording: "an integral constant expression with the value 0", but in K&R First edition, page 97, there is an explicit example which 0 is returned by a function returning a pointer to a character. Also, from the text "C guarantees that no pointer that validly points at data will contain zero, so a return value of zero can be used to signal an abnormal event,..." Also, from Appendix A, section 7.7 (Equality operators) "A pointer may be compared to an integer, but the result is machine dependent unless the integer is the constant 0. A pointer to which 0 has been assigned is guaranteed no to point to any object, and will appear to be equal to 0; in conventional usage, such a pointer is considered to be null." This implies to me that I can compare a pointer to any arbitrary expression which evaluates to 0 and have machine independent behavior. And from section 7.14, "it is guaranteed that the assignment of the constant 0 to a pointer will produce a null pointer distinguishable from a pointer to any object." Since the "BNF" for this section lists "lvalue = expression," I would assume that if expression evaluates to 0 then the pointer will produce a null pointer distinguishable from a pointer to any object. As far as ANSI C goes, I do not have easy access to the standard at the current time, nor the experience reading it. However, I would have expected this behavior to be brought forward (hopefully not a silly idea, but we'll see). This was a quick scan through K&R, so I hope I have not taken anything out of context. The only reason this objection stuck in my mind is that I had once thought of doing an implementation where NULL != 0, but after further reading convinced myself the implementation would be invalid. Share and Enjoy! Brian Haug Disclaimer: These opinions are mine alone.