[comp.std.c] Two questions about pointers

bdm@anucsd.oz (Brendan McKay) (10/18/89)

Here are two questions about the semantics of pointers, as defined by
the proposed ANSI standard.
 
(1) Is it true that two pointers of the same type which point to the
same object must compare equal?
 
(2) If two pointers of the same type compare equal, and each is cast
to a second pointer type, must the results compare equal?
 
The answer to both questions is "yes" in all implementations I can think of,
and will probably be "yes" in any implementation in which pointers are
represented just as machine addresses without meta-information.
 
The question is whether the proposed standard actually requires such
behaviour.  One can design weird pointer implementations which obey all the
rules relating to pointers, as far as I can see, but which do not have one
or both of these properties.  I can e-mail an example to anyone interested.
 
Note that Section 3.3.9 does not answer Question (1), at least not directly.
It gives the implication around the other way (equal pointers must point to 
the same object).  Question (2) is of particular interest if the second 
type is void*.
 
If I am correct in thinking that these properties are not required by the
standard, was that intentional, or was it an oversight?
If I'm not correct, please be gentle.
 
Brendan McKay,    bdm@anucsd.oz  or  bdm@anucsd.oz.au

gwyn@smoke.BRL.MIL (Doug Gwyn) (10/20/89)

In article <1989Oct18.121516.10695@anucsd.oz> bdm@anucsd.oz (Brendan McKay) writes:
>(1) Is it true that two pointers of the same type which point to the
>same object must compare equal?

A correct statement would be:

Two pointers that are constructed by following the rules in the Standard,
and for which the Standard says they point to the same object, are indeed
required to compare equal.

If you construct your pointers-to-the-same-object by sneaky means for
which the Standard makes no such guarantee, then they may compare
unequal.  Of course, in MOST implementations they would compare equal
in any case, but not in ALL implementations.

>(2) If two pointers of the same type compare equal, and each is cast
>to a second pointer type, must the results compare equal?

Again, if you have not stepped beyond the bound of what the Standard
guarantees, then the resulting pointers will also compare equal.

There are certain constraints that are required to be observed; for
example, alignments must be suitable.

>One can design weird pointer implementations which obey all the
>rules relating to pointers, as far as I can see, but which do not
>have one or both of these properties.

The implementations don't have to be especially weird.  Some
implementations are in fact expected to have to perform an
explict "normalizing" operation before testing for pointer equality.

bill@twwells.com (T. William Wells) (10/21/89)

In article <1989Oct18.121516.10695@anucsd.oz> bdm@anucsd.oz (Brendan McKay) writes:
: (1) Is it true that two pointers of the same type which point to the
: same object must compare equal?

Yes. See 3.3.9. Read further before disagreeing.

: (2) If two pointers of the same type compare equal, and each is cast
: to a second pointer type, must the results compare equal?

If there is an object there of that new type, yes. (E.g., unions.)
Or, if both pointers were originally of the new type, yes.

As near as I can tell, converting two pointers to a void* and then
testing for equality does not have to work, unless both were
void*'s to begin with.

: Note that Section 3.3.9 does not answer Question (1), at least not directly.
: It gives the implication around the other way (equal pointers must point to
: the same object).  Question (2) is of particular interest if the second
: type is void*.

You either have an old draft or you have not read yours carefully.
From the May 88 draft: "If two pointers point to the same object
or function, they compare equal."

---
Bill                    { uunet | novavax | ankh | sunvice } !twwells!bill
bill@twwells.com

bdm659@csc.anu.oz (10/23/89)

In article <1989Oct21.005145.23598@twwells.com>, bill@twwells.com (T. William Wells) writes:
> In article <1989Oct18.121516.10695@anucsd.oz> bdm@anucsd.oz (Brendan McKay) writes:
> : (1) Is it true that two pointers of the same type which point to the
> : same object must compare equal?
>
> Yes. See 3.3.9. Read further before disagreeing.
> You either have an old draft or you have not read yours carefully.
> From the May 88 draft: "If two pointers point to the same object
> or function, they compare equal."

Yes, alas, I have an old draft.  This addition places the semantics of
pointers on MUCH firmer footing.  Pointers to objects, anyway.
[Can someone please mail me the address for ordering the most recent draft?
 And the price.  Thanks muchly.]

> : (2) If two pointers of the same type compare equal, and each is cast
> : to a second pointer type, must the results compare equal?
>
> If there is an object there of that new type, yes. (E.g., unions.)
> Or, if both pointers were originally of the new type, yes.
> As near as I can tell, converting two pointers to a void* and then
> testing for equality does not have to work, unless both were
> void*'s to begin with.

Yes, that certainly follows from the sentence my copy doesn't have. Thanks.

As far as I can tell, my (old) draft contains absolutely nothing about the
meaning of the equality operators (==, !=) when applied to void* values.
Has that situation changed, or do we still have to guess?

Brendan McKay.  bdm@anucsd.oz  or  bdm@anucsd.oz.au