From -6318244068450322874
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,e94695845965b249
X-Google-Attributes: gidf78e5,public
X-Google-ArrivalTime: 1992-12-10 20:22:57 PST
Newsgroups: comp.std.c++
Path: sparky!uunet!stanford.edu!lucid.com!lucid.com!jss
From: jss@lucid.com (Jerry Schwarz)
Subject: Re: Zero-length structures and pointer comparisons
Message-ID: <1992Dec11.040713.13298@lucid.com>
Sender: usenet@lucid.com
Reply-To: jss@lucid.com (Jerry Schwarz)
Organization: Lucid, Inc.
References: <1992Dec2.111003.23102@jyu.fi> <1992Dec2.205330.10372@meaddata.com> <KANZE.92Dec10174617@slsvdnt.us-es.sel.de>
Date: Fri, 11 Dec 92 04:07:13 GMT
Lines: 25

In article <KANZE.92Dec10174617@slsvdnt.us-es.sel.de>, kanze@us-es.sel.de (James Kanze) writes:
|> 
|> |> int compare_pointers(const void *, const void *);
|> |>     /* Returns negative, zero, or positive in the same manner as strcmp().
|> |>        Perhaps in the interests of tradition and unreadability it should
|> |>        be called ptrcmp() instead of compare_pointers() :-) */
|> 
|> We already *have* this function.  Try the following:
|> 
|> 	void*	ptr1 ;
|> 	void*	ptr2 ;
|> 	int	cmp = memcmp( &ptr1 , &ptr2 , sizeof( void* ) ) ;
|> 
|> This works on all implementations I can think of.  Is it guaranteed?
|> (Ie: can pointers also hold undefined padding information which may
|> vary in a random fashion.  Note that "memcmp" is *not* guaranteed to
|> work on struct's.)
|

It is not guaranteed. Padding is certainly allowed in a void*, and
would be common on word addressed machines. Also I vaguely remember
an implementation of C for a segmented architectures that didn't
normalize pointers unless it had to (for comparisons). 

   -- Jerry Schwarz


