I just found a bug that, strangely, occurred only when optimization was turned on (g++ -O2
). It was an Arithmetic exception
in the following code, when interval
was set to zero (from a command line argument):
for(int i = 0; i < n; ++i) {
if((i + 1) % interval == 0) { // exception here
DoSomething();
}
}
It's obvious that modulo zero operation threw a division-by-zero exception, but why did this only occur when the code was compiled with optimization turned on?