Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!samsung!brutus.cs.uiuc.edu!jarthur!uci-ics!rfg From: rfg@ics.uci.edu (Ronald Guilmette) Newsgroups: comp.lang.c++ Subject: Re: references to dereferenced null pointers Message-ID: <25EB8EE8.8462@paris.ics.uci.edu> Date: 28 Feb 90 08:42:17 GMT References: <51083@microsoft.UUCP> Reply-To: rfg@ics.uci.edu (Ronald Guilmette) Organization: UC Irvine Department of ICS Lines: 60 In article <51083@microsoft.UUCP> jimad@microsoft.UUCP (Jim ADCOCK) writes: >/******** > >Is the following program legal, illegal, or undefined? > >[It compiles and runs "correctly" under cfront 2.0] Not surprizing at all. > >The program deals with the issue of the reference equivalent to a null >pointer -- ie what one would get if one could dereference a null pointer >and asssign the result to a reference. As the following shows, at least >under cfront 2.0, the dereference followed by assignment to a reference is >a conceptual dereference only --not actually done in machine code-- and >thus causes no null-pointer fault. While the following example is weak, >I claim assigning a "null" to a reference is useful when defining >iterators returning references rather than pointers. You have shown that it is useful, and you have done so quite clearly. Did anyone suggest that is wasn't useful? > >[I don't believe this issue is covered in the C++ references] Agreed, but then it doesn't have to be. In effect, on the fifth iteration you make `row' be a reference to a hypothetical `ow' object which happens to be located at address zero. You then check to see if the address of the refered to object is zero before you do anything rash to it. It's so simple that it is intutive. Why clutter up the manual? P.S. My rule of thumb is that references are exactly like const pointers only different (due to the implicit dereferencing). > >********/ > >class OW >{ > char ow[4]; >public: > OW(char* p) { strncpy(ow, p, 4); } > void print() { printf("%s ", ow); } >}; > >OW how("how"); OW now("now"); OW bow("bow"); OW wow("wow"); >OW* pow[] = { &how, &now, &bow, &wow, 0 }; > >int main() >{ > int i = 0; > while (1) > { > OW& row = *pow[i++]; > if (!&row) break; > row.print(); > } > return 0; >};