mercredi 29 juillet 2015

c++ reading with cin using previously read variable immediately

somewhere in my code for some online judge I have the following:

vector<int> arr(1 << 20);

int p;
cin >> p >> arr[p];

this gives me a segmentation fault when compiled with judge's (I don't have access to their machine though) compiler g++ 4.9.2 but not on my local Apple LLVM version 6.0 (clang-600.0.57)

however writing it as:

int p;
cin >> p;
cin >> arr[p];

works fine on the judge as well. Is this a known thing/bug? And why doesn't the first version work as expected?

Aucun commentaire:

Enregistrer un commentaire