Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!usenet.ins.cwru.edu!tut.cis.ohio-state.edu!cs.utexas.edu!uunet!brunix!sdm From: sdm@cs.brown.edu (Scott Meyers) Newsgroups: comp.lang.c++ Subject: Re: references to dereferenced null pointers Message-ID: <33188@brunix.UUCP> Date: 18 Mar 90 06:56:59 GMT References: <51083@microsoft.UUCP> <25EB8EE8.8462@paris.ics.uci.edu> <16179@haddock.ima.isc.com> <10582@alice.UUCP> Sender: news@brunix.UUCP Reply-To: sdm@cs.brown.edu (Scott Meyers) Organization: Brown University Department of Computer Science Lines: 40 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. 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(). If you let the evaluation semantics get any lazier, then you have to worry about this problem: int z = 1; // global variable int fse() { z = 0; } // function with side effects void foo(int y) { // function that doesn't use its argument if (z) doSomething(); else doSomethingElse(); } int x; foo( fse() ); When do you evaluate fse()? If you don't initialize y with its value at the beginning of foo(), then when? You've got to do it before z is evaluated... Scott sdm@cs.brown.edu