I recently read that signed integer overflow in C and C++ causes undefined behavior:
If during the evaluation of an expression, the result is not math
Integer overflow behaviour is not defined by the C++ standard. This means that any implementation of C++ is free to do whatever it likes.
In practice this means: whatever is most convenient for the implementor. And since most implementors treat int
as a twos-complement value, the most common implementation nowadays is to say that an overflowed sum of two positive numbers is a negative number which bears some relation to the true result. This is a wrong answer and it is allowed by the standard, because the standard allows anything.
There is an argument to say that integer overflow ought to be treated as an error, just like integer division by zero. The '86 architecture even has the INTO
instruction to raise an exception on overflow. At some point that argument may gain enough weight to make it into mainstream compilers, at which point an integer overflow may cause a crash. This also conforms with the C++ standard, which allows an implementation to do anything.
You could imagine an architecture in which numbers were represented as null-terminated strings in little-endian fashion, with a zero byte saying "end of number". Addition could be done by adding byte by byte until a zero byte was reached. In such an architecture an integer overflow might overwrite a trailing zero with a one, making the result look far, far longer and potentially corrupting data in future. This also conforms with the C++ standard.
Finally, as pointed out in some other replies, a great deal of code generation and optimization depends on the compiler reasoning about the code it generates and how it would execute. In the case of an integer overflow, it is entirely licit for the compiler (a) to generate code for addition which gives negative results when adding large positive numbers and (b) to inform its code generation with the knowledge that addition of large positive numbers gives a positive result. Thus for example
if (a+b>0) x=a+b;
might, if the compiler knows that both a
and b
are positive, not bother to perform a test, but unconditionally to add a
to b
and put the result into x
. On a twos-complement machine, that could lead to a negative value being put into x
, in apparent violation of the intent of the code. This would be entirely in conformity with the standard.