Xref: utzoo comp.sys.ibm.pc:22368 comp.sys.intel:610 Path: utzoo!utgpu!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!mailrus!ames!killer!chasm From: chasm@killer.DALLAS.TX.US (Charles Marslett) Newsgroups: comp.sys.ibm.pc,comp.sys.intel Subject: Re: correct code for pointer subtraction Summary: Longs are not the same as pointers though! Message-ID: <6473@killer.DALLAS.TX.US> Date: 17 Dec 88 21:16:38 GMT References: <15813@iuvax.cs.indiana.edu> <11477@dartvax.Dartmouth.EDU> Organization: The Unix(R) Connection, Dallas, Texas Lines: 31 In article <11477@dartvax.Dartmouth.EDU>, earleh@eleazar.dartmouth.edu (Earle R. Horton) writes: > In article <15813@iuvax.cs.indiana.edu> bobmon@iuvax.UUCP (RAMontante) writes: > ... > >So I agree with Bill Davidsen that the rigorous way to do this is to > >cast the pointers to huge, and subtract the casts. Viz., > ... > > I don't think anyone has suggested this, but perhaps casting to long > might be a better way to do this, and portable, too! I don't do much > 8086 stuff, but a long int is the same size as a long pointer on just > about every system I have run across. Subtraction of longs is never a > questionable operation, either, whereas pointer subtraction sometimes > is... Of course, pointers are not portably convertable to any kind of scalar (even longs) so the difference of two longs converted from two pointers is not necessarily going to give you the number of bytes seperating the two addresses -- this is true of almost any segmented address machine, and specifically it is true of the Microsoft and Borland C compilers. The point is, subtraction of longs is valid, conversion of pointers to longs (even if they are the same size) is not. And subtraction of pointers OUGHT TO BE portable (to reiterate my original statement, the Microsoft and Borland compilers are broken since they do not work "right"). I might add that both of these companies are in business to sell software -- tell them you do not like this overemphasis on fast over correct results and we may see improvement next time around! Charles Marslett chasm@killer.dallas.tx.us