bad optimization

Brian Budge
Tue Aug 20 18:34:00 GMT 2013

Hi all -

I'm using gcc 4.6.3 and I am getting (what I think is) a bad
optimization on a very simple program:

#include <iostream>
#include <cmath>

int main(int argc, char **args) {

    int i = -2147483648;
    if(std::abs(i) < 0) {
        std::cerr << "correct" << std::endl;
    else {
        std::cerr << "incorrect" << std::endl;

    return 0;

When compiled without optimization, this will print "correct".  When
compiled with optimization, "incorrect".  This works with clang.

I can easily work around this problem since there's only one value for
an int for which this will fail.

Is this an example of undefined behavior, or should I file a bug?


More information about the Gcc-help mailing list