Testing Equal Pointers (was: detecting invalid pointers)

Mark Brader msb at sq.com
Tue Mar 28 08:40:35 AEST 1989


> ... from comp.lang.c ...
> > It is possible however to have two pointers point to the same storage
> > location but which compare unequal.
> 
> Why is this dismal situation reflected in the pANS?  How can standard C
> let pointers to the same object compare unequal?  ...  STANDARD C
> PROVIDES NO WAY at all to tell if two pointers point to the same object!

Wrong (in pANS, "standard", C), no matter how loud you shout.  (I've added
a cross-posting back to comp.lang.c to point out that the compiler(s) referred
to in the original article would not be complying to the pANS in this respect.
Followups are directed to comp.lang.c also, since this article should close
the topic from a pANS viewpoint!)

The wording here did undergo some late editorial changes and may be
different between the October draft and the final December version of the
proposed standard.  Also, note that the discussion of equality is partly
under relational operators, rather than equality operators.  This is simply
because the presentation (in order of precedence) happens to get to them first.
Equality affects the relational operators too because the two expressions

		a <= b && !(a < b)
	and
		a == b

are equivalent in the absence of undefined behavior.

>From Section 3.3.8 "Relational operators", page 50, lines 7-10:

#  If two pointers to object or incomplete types both point to the same
#  object, or both point one past the last element of the same array
#  object, they compare equal.  If two pointers to object or incomplete
#  types compare equal, both point to the same object, or both point
#  one past the last element of the same array object.

There is a footnote about invalid prior operations and undefined behavior.

Then, in Section 3.3.9 "Equality operators", page 50, lines 28-36:

#  [These] operators are analogous to the relational operators except
#  for their lower precedence.  Where the operands have types and values
#  suitable for the relational operators, the semantics detailed in
#  Section 3.3.8 apply.
#
#  If two pointers to object or incomplete types are both null pointers,
#  they compare equal.  If two pointers to object or incomplete types
#  compare equal, they both are null pointers, or both point to the same
#  object, or both point one past the last element of the same array
#  object.  If two pointers to function types compare equal, either both
#  are null pointers, or both point to the same function.  If two pointers
#  to function types compare equal, either both are null pointers, or
#  both point to the same function.

There is a footnote which is redundant.

Mark Brader, SoftQuad Inc., Toronto, utzoo!sq!msb, msb at sq.com
	A standard is established on sure bases, not capriciously but with
	the surety of something intentional and of a logic controlled by
	analysis and experiment. ... A standard is necessary for order
	in human effort.				-- Le Corbusier



More information about the Comp.std.c mailing list