I have the following piece of code that I wrote in C. Its fairly simple as it just right bit-shifts x
for every loop of for.
int main() {
int x = 1;
for (int i = 0; i > -2; i++) {
x >> 2;
}
}
Now the strange thing that is happening is that when I just compile it without any optimizations or with first level optimization (-O
), it runs just fine (I am timing the executable and its about 1.4s
with -O
and 5.4s
without any optimizations.
Now when I add -O2
or -O3
switch for compilation and time the resulting executable, it doesn't stop (I have tested for up to 60s
).
Any ideas on what might be causing this?
i
will always be greater than -2 – Whipstitchi
is an int so -2 is actuallyINT_MAX -1
and notice thei > -2
instead ofi < -2
. – Closefittingint - 1
, I just did this :p – Closefittingfor
condition so you don't have that UD? – Eyestalk(int i = 0; i < INT_MAX - 1; i++)
and its fine with that. So yeah compiler is making it an infinite loop. – Closefittingx >> 2;
supposed to achieve? You throw away the result, and it has no effect. – Melar