I think my version of compiler is posessed? (1 && 0 detected in if statement)

Viewed 78
#include <iostream>
#include <vector>

bool contains(std::vector<int> list, int value){
    for(unsigned int i=0; i<list.size(); i++){
        if(list[i] == value) return 1;
    }
    return 0;
}

bool containsdouble(std::vector<double> list, double value){
    for(unsigned int i=0; i<list.size(); i++){
        if(list[i] == value) return 1;
    }
    return 0;
}

int main(){
    std::vector<int> step = {0};
    std::vector<double> value = {};
    value.push_back(double(4)/double(3));
    int i = 4;
    int j = 3;
    while(1){
        if(contains(step, (0)) && !containsdouble(value, double(i)/double(j))){
            std::cout << contains(step, (0)) << " && " << !containsdouble(value, double(i)/double(j)) << " caught on\ni = " << i << ", j = " << j;
            return 0;
        }
        i*=2; j*=2;
    }
    return 0;
}

This is the result

This is only when -O2 or -O3 and only occurs in some versions of compiler but not all versions of compiler. This result was done with the MinGW compiler, but again it only occurs in some versions of compiler.

So what is does is check if 0 is in step, which it is, and if not of 4.0/3.0 is in value, which it isn't. 1 && 0. Then it goes into the if statement anyway and gives the result.

0 Answers
Related