correct code for pointer subtraction

Ross Wetmore rwwetmore at grand.waterloo.edu
Mon Dec 26 16:30:30 AEST 1988


In article <2245 at iscuva.ISCS.COM> carlp at 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 at water.NetNorth
University of Waterloo          | rwwetmore at math.Uwaterloo.ca
Waterloo, Ontario N2L 3G1       | {uunet, ubc-vision, utcsri}
(519) 885-1211 ext 4719         |   !watmath!rwwetmore



More information about the Comp.lang.c mailing list