Path: utzoo!utgpu!watmath!grand!rwwetmore From: rwwetmore@grand.waterloo.edu (Ross Wetmore) Newsgroups: comp.lang.c Subject: Re: correct code for pointer subtraction Message-ID: <22905@watmath.waterloo.edu> Date: 26 Dec 88 06:30:30 GMT References: <597@mks.UUCP> <8377@bloom-beacon.MIT.EDU> <8455@sequent.UUCP> <2237@iscuva.ISCS.COM> <609@mks.UUCP> <2245@iscuva.ISCS.COM> Sender: daemon@watmath.waterloo.edu Reply-To: rwwetmore@grand.waterloo.edu (Ross Wetmore) Organization: U. of Waterloo, Ontario Lines: 36 In article <2245@iscuva.ISCS.COM> carlp@iscuva (Carl Paukstis) writes: >Eric Gisin at Mortice Kern Systems writes: >>How come I can't find a compiler that generates correct >>code for pointer subtraction in C on 8086s? >>Neither Turbo, Microsoft, or Watcom do it right. >> >>All of the compilers I tried computed a 16 bit difference, >>then sign extended it before dividing. >>This does not work if the pointers differ by more than 32K. > >(NOTE CRITICAL POINT FOR ERIC'S COMPLAINT: the difference between s and >s+10000 is 60,000 bytes - easily less that the 64K segment limit) The 64K segment limit has little to do with it. The 16 bit architecture ie 16 bit _int_ is the determining factor. >"Similarly, the C language defines the result of subtracting two >pointers as an _int_ value. ... as you posted. >Gentlepersons all: is this about the best job Microsoft could have done, >given the wonderfulness of Intel segmented address space? > >4) Avoid Intel-based systems whenever possible :-) ... but forgot when you let your prejudices take control. Have you tried the same on a 32 bit VAX where the addresses differ by more than 2**31 - 1? Oops, DEC ducked this one by putting the 'negative' addresses into a separate 'system' space so the address space is still describable by a positive _int_. However, is the point not clear ... ? >Carl Paukstis +1 509 927 5600 x5321 Ross W. Wetmore | rwwetmore@water.NetNorth University of Waterloo | rwwetmore@math.Uwaterloo.ca Waterloo, Ontario N2L 3G1 | {uunet, ubc-vision, utcsri} (519) 885-1211 ext 4719 | !watmath!rwwetmore