From -4468894033773227671
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,e94695845965b249
X-Google-Attributes: gidf78e5,public
X-Google-ArrivalTime: 1992-09-09 09:13:10 PST
Path: sparky!uunet!pipex!unipalm!uknet!mcsun!sunic!hagbard!loglule!jbn
From: jbn@lulea.trab.se (Johan Bengtsson)
Newsgroups: comp.std.c++
Subject: Re: Zero-length structures and pointer comparisons
Message-ID: <4945@holden.lulea.trab.se>
Date: 9 Sep 92 19:26:46 GMT
References: <9225302.22791@mulga.cs.mu.OZ.AU>
Organization: Telia Research AB, Aurorum 6, 951 75 Lulea, Sweden
Lines: 66
X-Newsreader: Tin 1.1 PL4

fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON) writes:
: 
: Secondly, some comments about C++.
: In ANSI C, pointer comparisons between pointers to unrelated objects (ie.
: objects that are not both members of the same aggregate) cause undefined
: behavior,

Small clarifications:

The ARM is explicit about the relational operators (<,>,<=,>=),
and then goes on to say the equality operators are "exactly analogous".
Anyway, comparison with pointers to members of the same aggregate are only
defined if they belong to the same access zone (simplified).  Also,
pointer comparison within the same array is supported.

:I believe. For example, the following program
: 	#include <assert.h>
: 	int main() {
: 		int x,y;
: 		assert(&x != &y);
: 	}
: should cause undefined behavior. [Is this correct?]

I'd say yes.  On a segmented architecture (can you say 'PC'?)
pointer comparison need not use the full 32 bits for pointer comparison,
instead a 16 bit offset may be used (note that this limits arrays to 64 KB).

I wonder if the 'PC' segmented architecture will reappear in the
first 64 bit processors (representing pointers as 32 bit offsets within
up to 2^32 segments)?  Does anyone have any info on this?

Anyway, the current rules do seem to cause trouble for the common (?)
idiom for operator =:

const T& operator = ( const T& t )
{
	if ( &t == this ) return;	// avoid self-assignment
	// ...
}

Is the above idiom broken?
Should the current rules be changed (inflicting a small performance penalty
on some machines)?

: Given that this is the case, it seems contradictory that repeated calls to
: operator new(0) are required to return pointers to "distinct objects".
: How could a conforming program possibly detect whether this requirement
: was actually met? Surely the following program
: 	#include <assert.h>
: 	int main() {
: 		assert(operator new(0) != operator new(0));
: 	}

If sizeof(long) >= sizeof(void*), then you should be able to
test like this:

assert( (long)p1 != (long)p2 );

since a pointer stored in an integer variable must keep all information
needed to restore the pointer (if the integer type is large enough).

-- 
--------------------------------------------------------------------------
| Johan Bengtsson, Telia Research AB, Aurorum 6, S-951 75 Lulea, Sweden  |
| Johan.Bengtsson@lulea.trab.se; Voice:(+46)92075471; Fax:(+46)92075490  |
--------------------------------------------------------------------------


