g++: beware of the signed overflow

Правка en1, от MikeMirzayanov, 2016-05-31 17:47:10

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.

Этот код при компиляции с -O2 в современном g++ приводит к бесконечному циклу. Suddenly, right?

Теги g++, ub

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en2 Английский MikeMirzayanov 2016-05-31 18:26:45 85
en1 Английский MikeMirzayanov 2016-05-31 17:47:10 689 Initial revision for English translation
ru1 Русский MikeMirzayanov 2016-05-31 17:43:14 634 Первая редакция (опубликовано)