Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!usc!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!aplcen!uunet!microsoft!jimad From: jimad@microsoft.UUCP (Jim ADCOCK) Newsgroups: comp.lang.c++ Subject: references to dereferenced null pointers Message-ID: <51083@microsoft.UUCP> Date: 27 Feb 90 18:42:23 GMT Reply-To: jimad@microsoft.UUCP (Jim ADCOCK) Organization: Microsoft Corp., Redmond WA Lines: 41 /******** Is the following program legal, illegal, or undefined? [It compiles and runs "correctly" under cfront 2.0] 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. [I don't believe this issue is covered in the C++ references] ********/ 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; };