checking for overflow in C

Jim Shankland jas at ernie.Berkeley.EDU
Mon May 8 09:56:37 AEST 1989


In article <10218 at smoke.BRL.MIL> gwyn at brl.arpa (Doug Gwyn) writes:
>In article <13367 at dartvax.Dartmouth.EDU> shallit at eleazar.dartmouth.edu (Jeffrey Shallit) writes:
>>... enlighten me about the officially approved way of checking overflow when
>>multiplying two integers.
>...
>You're looking for tactics when what is needed is better strategy.
>How did you get your algorithm into the state where an overflow is
>even possible?  Sounds to me like the algorithm needs to be better
>engineered.

I can think of lots of cases where the operands of an arithmetic operation
aren't known until run-time.  The only way to ensure that the operation
won't overflow is to perform some check on the operands, which is exactly
what he was inquiring about.  I don't know how else he might "better
engineer" his algorithm to avoid the problem.

I understand the reasons for C's overflow semantics (or lack of them, if
you will).  Nonetheless, while it's not a problem most of the time,
when it is a problem, it's a big one.  Operations like multiplication
end up becoming a subroutine -- often one written in assembler.

Jim Shankland
jas at ernie.berkeley.edu

"Blame it on the lies that killed us, blame it on the truth that ran us down"



More information about the Comp.lang.c mailing list