On 23/07/11 21:13, Agner Fog wrote: > I have a program where I check for integer overflow. The program failed, > and I found that gcc has optimized away the overflow check. I filed a > bug report and got the answer: >> Integer overflow is undefined. You have to check before the fact, or compile >>> with -fwrapv. > ( http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49820 ) > > 5). I have tested many different C++ compilers, and gcc turned out to be > the one that optimizes best. Well yes, and one of the reasons for this is that we take advantage of integer overflow being undefined. There is an entire class of optimizations (loop induction variable optimization) that is difficult of impossible without taking advantage of this. We don't do this kind of thing without good reason. We can't promise you that in every case gcc will do what you would like it to do, However, we have thought long and hard about it. Andrew.