Path: utzoo!utgpu!jarvis.csri.toronto.edu!clyde.concordia.ca!uunet!microsoft!jimad From: jimad@microsoft.UUCP (Jim ADCOCK) Newsgroups: comp.lang.c++ Subject: Re: references to dereferenced null pointers Message-ID: <52081@microsoft.UUCP> Date: 7 Mar 90 18:42:20 GMT References: <51083@microsoft.UUCP> <25EB8EE8.8462@paris.ics.uci.edu> Reply-To: jimad@microsoft.UUCP (Jim ADCOCK) Organization: Microsoft Corp., Redmond WA Lines: 76 In article <25EB8EE8.8462@paris.ics.uci.edu> rfg@ics.uci.edu (Ronald Guilmette) writes: >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. Always surprising 1/2 :-) >>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? No, but I suggest that it has not yet been defined as "legal", and in fact there are hints in the reference that this behaviour should presently be considered "undefined." If this behaviour is defined as "legal," then this is going to have impact on mapping C++ to systems with [hardware support for] typed pointers, such as "Rekursiv." >>[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? Simple, but wrong. Reading the reference, I find: "A constant expression that evaluates to 0 is converted to a pointer, commonly called the null pointer. It is guaranteed that this value will produce a pointer distinguishable from a pointer to any object." If the null pointer does not point to any object, then why should I assume that I can take the address of that "object" which isn't an object? What guarantee is there that an object that doesn't exist even has an address? And why should that address be "0" ??? It may be that many present systems represent *some* C++ null pointers as 32 bits all zero, but this is not the same as saying making such an assumption is portable to future systems. In particular, a system with [hardware support for] typed pointers would not have null pointers represented as 32 bits all zero. Also, note that the "0" that creates a null pointer must be a constant expression -- allowing a compiler to construct a special representation for null pointers at compile time. Also note: This means the fairly common "C" coding technique of assigning to a pointer a runtime expression that evaluates to zero is not guaranteed to be legal. And assigning one null pointer to a different type of pointer need not keep the same bit representations. >P.S. My rule of thumb is that references are exactly like const pointers >only different (due to the implicit dereferencing). Like many rules of thumb, this one is quite wrong. References are not even first class citizens. Like register variables, references don't even have an address. sizeof(aRefToOb) is the sizeof(Ob), whereas sizeof(aPtrToOb) is typically the native size of a pointer on your system [2 or 4, typical]. Further, using references may generate temporaries where using pointers does not. So, I interpret your response to be: "This is presently undefined, but who cares, its trivial." Well, I care, and I claim its not a trivial issue, but rather has important impact for the mapping of C++ onto systems with typed pointers. If making null references *is* legal, let someone in the know state so uneqivocally, and I can get on with my programming. I'd like to see this made explicetly legal -- but doing so may have a sharp negative impact on systems with typed pointers.