Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watnot!watmath!clyde!cbatt!ucbvax!sdcsvax!sdchem!tps From: tps@sdchem.UUCP Newsgroups: comp.lang.c Subject: Re: Pointer Comparison and Portability Message-ID: <638@sdchema.sdchem.UUCP> Date: Thu, 19-Feb-87 02:56:34 EST Article-I.D.: sdchema.638 Posted: Thu Feb 19 02:56:34 1987 Date-Received: Fri, 20-Feb-87 01:48:32 EST References: <416@vaxine.UUCP> <454@mipos3.UUCP> <636@sdchema.sdchem.UUCP> <541@viper.UUCP> Sender: news@sdchem.UUCP Reply-To: tps@sdchemf.UUCP (Tom Stockfisch) Organization: UC San Diego Lines: 74 In article <541@viper.UUCP> john@viper.UUCP (John Stanley) writes: >In article <636@sdchema.sdchem.UUCP> tps@sdchemf.UUCP (Tom Stockfisch) writes: > > > >That is, even on a segmented architecture, when pointers get passed to a > >subroutine, they MUST have distinct addresses -- how else can the subroutine > >know what area of memory to access? > > > > Tom, on segmented arcitecture machines you -could- have two distinctly >different pointer values which point to the same memory space... >... > In the example he gave: > char p[16], q[16]; >... > This means the address of &p[0] could be 0001:0000 (segment:offset) and the >address of &q[0] could be 0002:0000. Now, given that a compiler -could- >allocate and address the two arrays in this manner, the address for the >memory location &p[17] (incorrect, but technicaly a legal memory reference) >would be 0001:0010. The byte of memory addressed by q[0] and p[17] would >be the exact same byte, but the two pointers 0002:0000 and 0001:0010 would be >different. &p[17] is illegal (or at least undefined) in C, given the above definition. If I compared this to any other pointer value (even inside p[]) I would not expect to get anything well defined. > Getting back to the original starting question asked by Neil Webber: >>>Consider the following C function: >>> same_char (p, q) >>> char *p; >>> char *q; >>> { >>> return (p == q); >>> } >>>Does this function only return a non-zro value when p and q point >>>to the same physical character? > The answer to the exact wording of the question is YES... However.... >saying that the function will ONLY return a non-zero value when the pointers >match is not the same as "Does the function ALWAYS return a non-zero value >when p and q point to the same physical character?" The answer to the latter >question is NO. Judging from your above comments, I assume that what you mean by this is that if you pass &p[16] and &q[0] to same_char(), they might refer to the same physical memory but have a different value. Since there is no guarantee in C that adjacent definitions wind up adjacent in memory, I would say the result of same_char( &p[16], &q[0] ) would be undefined (by C). It would make no more sense to argue about what same_char() should return in this case than if you had called char *r = p; same_char( r++, r++ ) So I would say that if you pass *defined* pointer values to same_char() it will return 1 if and only if they refer to the same memory location, and will return 0 otherwise. || Tom Stockfisch, UCSD Chemistry tps%chem@sdcsvax.UCSD