g++: beware of the signed overflow

Revision en2, by MikeMirzayanov, 2016-05-31 18:26:45

Hi!

I know that it will be obvious for real C++-masters, but for me it is small surprise. I used to think that signed overflow in C ++ does not cause real undefined behavior on a particular platform. That is, it is clear that depending on the big-endianness/little-endian result may be different.

I found short and funny example:

#include <iostream>
int main()
{
    for (int i = 0; i < 300; i++)
        std::cout << i << " " << i * 12345678 << std::endl;
}

Being compiled with -O2 on modern g++ it will run infinitely. Suddenly, right?

Tags g++, ub

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English MikeMirzayanov 2016-05-31 18:26:45 85
en1 English MikeMirzayanov 2016-05-31 17:47:10 689 Initial revision for English translation
ru1 Russian MikeMirzayanov 2016-05-31 17:43:14 634 Первая редакция (опубликовано)