Re: Integer Overflow



On Dec 29, 5:41 pm, jacob navia <ja...@xxxxxxxxxxxx> wrote:

What bothers me is that you seem so upset that I offer a SOLUTION
instead of just allowing UB. Maybe you can offer a BETTER solution?

Upset about offering a solution? No, upset about pretending to offer a
solution. Without thinking things through. As an example, in

c = a + b;
error = (_overflow() | (c + 3 == x));

it is unspecified which is the "last operation" before the call to
_overflow(), whether it is the a + b or the c + 3.

And what does "this is invalid" mean? Does it mean the sequence

d = a + b + c;
if (_overflow ()) printf ("There was an overflow\n");

invokes undefined behaviour? Or what?

Better solution:

bool _overflow (int arg);

Definition: The _overflow function will return 0 or 1 and have no
further side effects. The argument to the _overflow function shall not
include any function calls. The result is 1 if any overflow happened
during the evaluation of the function argument as the result of
evaluating one of the operators +, -, *, / or % with integer operands,
and 0 otherwise.

So you just write:

if (_overflow (d = a + b + c)) printf ("There was an overflow\n");

Not quite as simple for the compiler, but then life isn't always
simple.
.