Testing Equal Pointers (was: detecting invalid pointers)

Doug Gwyn gwyn at smoke.BRL.MIL
Mon Mar 27 13:16:15 AEST 1989


In article <375 at sdti.SDTI.COM> turner at sdti.SDTI.COM (Prescott K. Turner, Jr.) writes:
-The quotes below are from comp.lang.c.

You shouldn't believe everything you read in comp.lang.c.

-In article <1989Mar21.085704.15894 at ateng.ateng.com> chip at ateng.ateng.com (Chip Salzenberg) writes:
-> For totally correct comparisons of all pointers, it's necessary
-> to normalize them by hand, or be sure that they are cast to "huge *" when
-> any pointer arithmetic is done.  Otherwise, the different combinations of
-> segment+offset that actually refer to the same address do not compare equal.
-In article <16039 at cup.portal.com> Kevin_P_McCarty at cup.portal.com writes:
-> It is possible however to have two pointers point to the same storage
-> location but which compare unequal.  A one-to-one mapping between
-> pointers and storage locations is not required.

If these are talking about ANSI C, they are incorrect.
(Except that certain operations not permitted of conforming applications
might produce pointers that accidentally refer to the same location; but
since that would be totally erroneous code, you shouldn't worry about it.)

-Why is this dismal situation reflected in the pANS?  How can standard C
-let pointers to the same object compare unequal?  Even in Microsoft C
-large model there are ways to be sure that your pointer comparisons for
-equality/inequality will yield the appropriate result.  
-But STANDARD C PROVIDES NO WAY at all to tell if two pointers point to the
-same object!  In fact, a compiler could implement (p1==p2) as
-(p1==0 && p2==0) and still pass all tests for standard conformance.
-Any program is not strictly conforming if it tests for equality of
-pointers, gets a 0 result, and then proceeds on the basis that those
-pointers do not point to the same object.

That's all wrong.  Pointers to the same object compare equal, and object
pointers that compare equal are either both null pointers or both refer
to the same object (or one past it).  Similarly for function pointers.
See sections 3.3.8 and 3.3.9 in the pANS.  Null pointers compare unequal
to pointers to objects or functions; see section 3.2.2.3.



More information about the Comp.std.c mailing list