This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Integer overflow

Hello to everyone.

When I have to calculate the average of two integer numbers, first i have to add them. Sometimes an overflow occurs. One technique maybe to compare the result to one of the numbers, if result is lower, an overflow is recognized.

Is this good style or better use Assembler?

Parents
  • "One technique maybe to compare the result to one of the numbers, if result is lower, an overflow is recognized."

    Bad style for C.

    According to the C Standard, integer overflow can invoke undefined behavior, regarding which, the Standard has a note:

    "Possible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message)."

    From the Standard's perspective, having monkeys fly out your nose would be allowed too.

    It is commendable that you are trying to work through the issue of integer overflow. The problem is that your implementation can cause overflow. Once you have caused an overflow, you have undefined behavior. If you want to deal with the issue of integer overflow in a rigorous way, you can't expect to detect it after the fact, you must avoid overflow before causing undefined behavior. This typically involves using wider types as has been suggested, or checking whether one operand is less than the integer distance between the other operand and INT_MAX (or INT_MIN), or converting to unsigned arithmetic and examining the result.

    I'd suggest searching Google Groups' Usenet archive for "avoiding integer overflow". This will bring up some relevant comp.lang.c(.moderated) discussions amongst folks smarter than I and give you a better idea what I'm talking about.

Reply
  • "One technique maybe to compare the result to one of the numbers, if result is lower, an overflow is recognized."

    Bad style for C.

    According to the C Standard, integer overflow can invoke undefined behavior, regarding which, the Standard has a note:

    "Possible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message)."

    From the Standard's perspective, having monkeys fly out your nose would be allowed too.

    It is commendable that you are trying to work through the issue of integer overflow. The problem is that your implementation can cause overflow. Once you have caused an overflow, you have undefined behavior. If you want to deal with the issue of integer overflow in a rigorous way, you can't expect to detect it after the fact, you must avoid overflow before causing undefined behavior. This typically involves using wider types as has been suggested, or checking whether one operand is less than the integer distance between the other operand and INT_MAX (or INT_MIN), or converting to unsigned arithmetic and examining the result.

    I'd suggest searching Google Groups' Usenet archive for "avoiding integer overflow". This will bring up some relevant comp.lang.c(.moderated) discussions amongst folks smarter than I and give you a better idea what I'm talking about.

Children