Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!iuvax!cica!tut.cis.ohio-state.edu!att!dptg!ulysses!andante!alice!shopiro From: shopiro@alice.UUCP (Jonathan Shopiro) Newsgroups: comp.lang.c++ Subject: Re: references to dereferenced null pointers Message-ID: <10595@alice.UUCP> Date: 18 Mar 90 17:19:38 GMT References: <51083@microsoft.UUCP> <25EB8EE8.8462@paris.ics.uci.edu> <33188@brunix.UUCP> Organization: AT&T Bell Laboratories, Murray Hill NJ Lines: 49 In article <33188@brunix.UUCP>, sdm@cs.brown.edu (Scott Meyers) writes: > In article <10582@alice.UUCP> shopiro@alice.UUCP (Jonathan Shopiro) writes: > > void h(int& r) > > { > > if (&r == 0) ... > > } > > > > int* x = 0; > > h(*x); > >I think the fundamental issue here is when is a pointer dereferenced? > >(Since it is clearly illegal to dereference the null pointer). I don't > > Two comments: > > 1. In h(), r is a reference to an object. By definition, no object > can have an address that is equal to 0, so (&r == 0) should always > fail. But the question is, must a reference actually refer to an object? Just as a pointer can point to an object or to no-object, a reference could refer to an object or to no-object. Then, if a reference referred to no-object, (&r == 0) would succeed. Note that this has nothing whatsoever to do with the representation of a null pointer. If the reference r refers to no-object, then &r is the null pointer (however it is represented). It does have something to do with the representation of a reference. The representation would have to be capable of storing a reference to no-object, as well as a reference to any object. Since references are generally represented as pointers, this shouldn't be a problem. > 2. In the call h(*x), we don't know precisely when *x is evaluated, > but since r is initialized with that value, it must occur prior to > executing any of the body of h(). The real question is not _when_ is *x evaluated, but __how_. I claim that *x is evaluated differently in int i = *x; // eval *x for rvalue than in int* p = &*x; // eval *x for lvalue and if x is the null pointer, the first is illegal and the second should be legal, since x is never dereferenced. The call h(*x) is analogous to the second example. -- Jonathan Shopiro AT&T Bell Laboratories, Warren, NJ 07060-0908 research!shopiro (201) 580-4229