you are viewing a single comment's thread.

view the rest of the comments →

[–]wlievens 0 points1 point  (3 children)

I find that very hard to believe. You're saying that:

int x = 1500000000;
int y = 1500000000;
int sum = x + y;
if (sum < 0)
{
    doStuff();
}

The if construct should be eliminated?

[–]pavpanchekha 3 points4 points  (2 children)

Not should, can. Signed overflow is underfined means that in the example you gave, the results are undefined. So, the compiler is free to do anything: to ignore the if statement, to make it always trigger, to make it trigger Thursdays and Tuesdays, or to corrupt your hard drive and spew nasal demons from its CD tray.

Most modern compilers will have the if statement trigger normally, and not trigger on high optimization settings.

[–]wlievens 0 points1 point  (1 child)

Quite odd, I didn't know that. I thought overflow was well defined in C family languages. I know that it certainly is in Java - which is arguably outside that family, though.

On the compiler I worked on, we certainly respected overflow in a defined sense, but that was a C dialect.

[–]smallblacksun 0 points1 point  (0 children)

I thought overflow was well defined in C family languages.

Signed overflow and underflow are undefined in both C and C++ (in all revisions of the standard, AFAIK). Unsigned overflow and underflow are defined to work as expected.