I have compiled and run the following C++ code with g++ version 4.8.2:
vector<int> ivec{0,1,2};
int& iref = ivec[1];
for (int i=3;i<100;++i)
ivec.push_back(i);
iref = 10;
cerr<<"After Error"<<'\n';
return 0;
The program will crash as expected at line iref = 10;
, because the reference is invalidated. But the string "After Error"
gets printed. Why?
The answer to this question is important to me, because most of the time I use cout
or cerr
to find the line causing a run-time error.