samedi 27 avril 2019

What does GCC compiler do to type conversion? Why the output on mac and linux are different?

I did the type conversion of variable b(declaration outside the scope) in a scope and give a new val to b, and when the scope ends, the val of b seem to be wrong.

This happens on my macbook, which version of gcc is gcc-8 (Homebrew GCC 8.3.0) 8.3.0. I tried the same code on my linux laptop whose gcc version is 5.4.0 and the code runs well.

vector<int> a = {1,2,3,4};
    int b;
    {
        size_t i = 0, b = a[i];
        //this time type of b is size_t

        ++i;
        b = a[i];

    }
    cout << "b = " << b << endl;

On my mac, the result is b = 0 On Ubuntu 16, the result is b = 1

What is the difference between two version of gcc on type conversion?

Or it is a bug?

Aucun commentaire:

Enregistrer un commentaire