Path: utzoo!attcan!uunet!lll-winken!lll-ncis!helios.ee.lbl.gov!nosc!ucsd!rutgers!njin!princeton!phoenix!rjchen From: rjchen@phoenix.Princeton.EDU (Raymond Juimong Chen) Newsgroups: comp.sys.ibm.pc Subject: Re: correct code for pointer subtraction Message-ID: <5158@phoenix.Princeton.EDU> Date: 5 Jan 89 18:51:03 GMT References: <607@sactoh0.UUCP> Organization: Princeton University, NJ Lines: 39 From article <607@sactoh0.UUCP>, by smcroft@sactoh0.UUCP (Steve M. Croft): > In article <5137@phoenix.Princeton.EDU>, rjchen@phoenix.Princeton.EDU (Raymond Juimong Chen) writes: >> > There is an overflow in there. >> >> Exactly. And the compiler should be smart enough to emit code to handle >> the overflow so as to generate the "correct" answer. > > At what point should the compiler not be expected to fix overflow problems? > Whadda 'bout: > > int a; > a = (30000*30000*30000*30000*30000*30000)/(30000*30000*30000*30000*30000) > > This involves more than resolving the carry bit, as was earlier suggested.. But this involves an explicit calculation which overflows an int, and is therefore ``implementation-defined''. The same goes for division by zero, or generating a floating-point result which is too big. If the answer is too big, then the result is ``implementation-defined''. On the other hand, the subtraction of two pointers to the same array is PROMISED by K&R to return a "correct" answer. Remember: I put the word "correct" in quotation marks to emphasize that it is guaranteed by K&R, and therefore any compiler which returns anything different does not observe the K&R specifications. Whether the K&R specifications are silly, outdated, or unimplementable is completely irrelevant. (The thing about the carry bit was to suggest a possible method of returning the "correct" answer for pointer subtraction. It was certainly not pretending to be a solution to all overflow problems! As other folks have mentioned, extending to a 32-bit difference also solves the problem.) -- Raymond Chen UUCP: ...allegra!princeton!{phoenix|pucc}!rjchen BITNET: rjchen@phoenix.UUCP, rjchen@pucc ARPA: rjchen@phoenix.PRINCETON.EDU, rjchen@pucc.PRINCETON.EDU "Say something, please! ('Yes' would be best.)" - The Doctor