Xref: utzoo comp.lang.c++:6794 comp.std.c:2600 Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!decwrl!amdcad!nucleus!tim From: tim@nucleus.amd.com (Tim Olson) Newsgroups: comp.lang.c++,comp.std.c Subject: Re: references to dereferenced null pointers Message-ID: <29472@amdcad.AMD.COM> Date: 12 Mar 90 15:01:04 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> <1990Mar11.222634.2701@almira.uucp> Sender: news@amdcad.AMD.COM Reply-To: tim@amd.com (Tim Olson) Organization: Advanced Micro Devices, Inc., Austin, Texas Lines: 41 Summary: Expires: Sender: Followup-To: In article <1990Mar11.222634.2701@almira.uucp> haug@Columbia.NCR.COM (Brian Haug) writes: | In article <1990Mar11.015305.28264@utzoo.uucp> henry@utzoo.uucp (Henry Spencer) writes: | >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. That is a correct use. Just as in an assignment to a pointer type, the compiler knows that it is to coerce the integral constant 0 to the machine-specific nil representation for that pointer type. This differs from the case that was being discussed, in that the return value is specifically declared to be a pointer type. In the previous discussion, the return value was declared "int". | 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. Not unless it is a constant expression that evaluates to 0. Constant expressions are a subset of expressions that involve only integral constants and certain operators -- function calls are not allowed. -- Tim Olson Advanced Micro Devices (tim@amd.com)