I found this code can not finish in time, anyone knows why it can't finished in 10 iterations? Is it because of something happened to -O2? I do not think the overflow of n will affect the loop if the compiler haven't done some weird work. Sometimes, we need overflow, for example, in rolling hash of string, but why it works properly there and not bellow?
Please help!!!
#include <bits/stdc++.h>
using namespace std;
int main() {
int cnt = 0;
for(int n = (1U << 31) - 10; cnt < 10; n++) {
cout << n << endl;
cnt++;
}
return 0;
}
Because signed integer overflow is undefined behavior, the compiler is free to assume that it doesn't happen, i.e. cnt is always less than 10. When you need overflow, use unsigned integers.
The signed integer overflow is undefined? What's undefined means here? Is it means the program will out of control even this integer's value isn't important? why cnt is always < 10? you can try to output it, it can bigger than 10, but the loop still do not end, it seems the compiler ignores the end condition of this loop.
When your program contains an undefined behavior , a standards conforming compiler can do anything it wants (result of compilation is undefined; in particular, your program could output Shakespeare's Hamlet to stdout). This rule is important to enable many compiler optimizations.
What Every C Programmer Should Know About Undefined Behavior
In this particular case, here's what the compiler thinks:
for(int n = (1U << 31) - 10; true; n++)
.This really really helps!!!! Thank you :D