Xref: utzoo comp.sys.ibm.pc:22255 comp.sys.intel:603 Path: utzoo!dciem!nrcaer!sce!msn034!kim From: kim@msn034.misemi (Kim Letkeman) Newsgroups: comp.sys.ibm.pc,comp.sys.intel Subject: Re: correct code for pointer subtraction Message-ID: <424@msn034.misemi> Date: 13 Dec 88 17:44:18 GMT Article-I.D.: msn034.424 References: <597@mks.UUCP> <8377@bloom-beacon.MIT.EDU> <8455@sequent.UUCP> Organization: MITEL Corporation, Kanata, Ontario, Canada. Lines: 31 Summary: IBM oddities don't really count ... In article <8455@sequent.UUCP>, sch@sequent.UUCP (Steve Hemminger) writes: > I believe the ANSI std and K&R say pointer subtraction is only allowed inside > one data structure/array. Since the small/medium/large model only allows > arrays <64K, only a 16bit result needs to be computed. Any code that > depends on subtracting pointers into two totally seperate arrays is > non-portable. K&R A6.6 Pointers and Integers and A7.7 Additive Operators states that adding or subtracting pointers (with integers or other pointers within the same array) gives you the displacement (as in relative record number). ptr(obj2)-ptr(obj1) is always 1. If you want to get a physical (byte) offset, you would likely have to cast to int or longint, which would definitely be non portable. Small data models (tiny, small, medium) use 16 bit pointers as offsets from DS. Large data models (compact, large, huge) use 32 bit pointers consisting of the segment and offset. The huge model is special in that all pointer references call routines that normalize the result (smallest possible segment value). This allows pointer addition/subtraction without worrying about segment problems. The small data models and the huge model appear to be the only really safe models for heavy pointer arithmetic where byte displacements are being calculated and used. The language used in the above comment is a bit loose and ambiguous. You should tighten it up a bit to avoid misunderstandings. Kim