Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!panda!teddy!jpn From: jpn@teddy.UUCP (John P. Nelson) Newsgroups: comp.lang.c,comp.unix.wizards Subject: Re: pointer alignment when int != char * Message-ID: <4330@teddy.UUCP> Date: Fri, 4-Sep-87 17:21:37 EDT Article-I.D.: teddy.4330 Posted: Fri Sep 4 17:21:37 1987 Date-Received: Sat, 5-Sep-87 19:47:40 EDT References: <493@its63b.ed.ac.uk> <6061@brl-smoke.ARPA> <3812@spool.WISC.EDU> <2130@umn-cs.UUCP> <6357@brl-smoke.ARPA> <286@halley.UUCP> Reply-To: jpn@teddy.UUCP (John P. Nelson) Organization: GenRad, Inc., Concord, Mass. Lines: 27 Xref: mnetor comp.lang.c:4170 comp.unix.wizards:4087 [Lots of stuff about subtracting pointers deleted...] > >It seems to me that everyone is ignoring his Ed's point. Let's say a function >is to take two pointer arguments, a pointer to a string and a pointer >into the string. What you say seems to indicate that arithmetic expressions >involving both pointers, such as their difference, will produce unpredictable >results at execution time, because the called function has no way of knowing >whether the pointers are actually to the same "string" or not. This is the wrong way of looking at it. The compiler is free to ASSUME that the two pointers point to members of a single array: otherwise the program would not be a "strictly conforming program". The compiler does not HAVE TO decide if the two pointers can be subtracted: The standard says that it is the programmer's problem to assure this. The compiler is free to do just about anything if the program is incorrect. In other words, in a segmented architecture (like the 8086), the compiler can ASSUME that the segments are identical, and perform the computation on the two pointer offsets, because if the segments are different, the program is not correct. In a tagged architecture (or an interpreted environment), the fact that the two pointers to not point to members of an array might be detected at RUN TIME. The standard says that this is a perfectly valid approach. If you have a linear address space, where all addresses fit into an integer of some kind, the standard does not forbid returning the distance between two arbitrary pointers. You simply cannot assume that this will work for all implementations.