Skip to content

Instantly share code, notes, and snippets.

@soonhokong
Last active February 25, 2016 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soonhokong/8b1699877197e66160b6 to your computer and use it in GitHub Desktop.
Save soonhokong/8b1699877197e66160b6 to your computer and use it in GitHub Desktop.

Clang Version

Debian clang version 3.3-debian16precise1 (branches/release_33) (based on LLVM 3.3)
Target: x86_64-pc-linux-gnu
Thread model: posix

Code (test.cpp)

#include <vector>
#include <iostream>

using std::vector;
using std::cout;
using std::endl;

struct P {
    vector<unsigned> v1;
    vector<unsigned> v2;
    P(unsigned l): v1(l), v2(l) {
        for (unsigned i = l; i > 0; --i) {
            v1[i - 1] = v2[i - 1] = i - 1;
        }
        for (unsigned i = 0; i < l; i++) {
                cout << "v1[" << i << "] = " << v1[i] << "\t";
                cout << "v2[" << i << "] = " << v2[i] << endl;
        }
    }
};

int main() {
    P p(9);
    return 0;
}

Compiler Option

clang++-3.3 -O3 test.cpp

Correct Output (without -O3)

v1[0] = 0	v2[0] = 0
v1[1] = 1	v2[1] = 1
v1[2] = 2	v2[2] = 2
v1[3] = 3	v2[3] = 3
v1[4] = 4	v2[4] = 4
v1[5] = 5	v2[5] = 5
v1[6] = 6	v2[6] = 6
v1[7] = 7	v2[7] = 7
v1[8] = 8	v2[8] = 8

Wrong Output (with -O3)

v1[0] = 0	v2[0] = 0
v1[1] = 2	v2[1] = 1
v1[2] = 1	v2[2] = 2
v1[3] = 4	v2[3] = 3
v1[4] = 3	v2[4] = 4
v1[5] = 6	v2[5] = 5
v1[6] = 5	v2[6] = 6
v1[7] = 8	v2[7] = 7
v1[8] = 7	v2[8] = 8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment