Shibli Shibli - 5 months ago 29
C++ Question

Vector going out of bounds without giving error

I have a

. I check its size which is 6 but when I try to access
to check whether it will give error, I get no error but some number instead. Should not it give an error?

edit: something like:

struct Element
std::vector<double> face;

int main()
Element elm;

.... // insert 6 elements into elm.face

std::cout << elm.face.size() << std::endl; // answer is 6
std::cout << elm.face[6] << std::endl; // answer is some number

Answer Source

STL vectors perform bounds checking when the .at() member function is called, but do not perform any checks on the [] operator.

When out of bounds, the [] operator produces undefined results.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download