Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!pasteur!ames!haven!adm!smoke!gwyn From: gwyn@smoke.BRL.MIL (Doug Gwyn ) Newsgroups: comp.std.c Subject: Re: Testing Equal Pointers (was: detecting invalid pointers) Message-ID: <9930@smoke.BRL.MIL> Date: 27 Mar 89 03:16:15 GMT References: <1989Mar21.085704.15894@ateng.ateng.com> <16039@cup.portal.com> <375@sdti.SDTI.COM> Reply-To: gwyn@brl.arpa (Doug Gwyn (VLD/VMB) ) Organization: Ballistic Research Lab (BRL), APG, MD. Lines: 36 In article <375@sdti.SDTI.COM> turner@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@ateng.ateng.com> chip@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@cup.portal.com> Kevin_P_McCarty@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.