Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vector going out of bounds without giving error

Tags:

I have a std::vector. I check its size which is 6 but when I try to access vec[6] 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 } 
like image 387
Shibli Avatar asked May 18 '13 02:05

Shibli


People also ask

Is vector guaranteed to be contiguous?

Yes, the elements of a std::vector are guaranteed to be contiguous.

What happens when a vector goes out of scope?

The second way to delete a vector is just to let it go out of scope. Normally, any non-static object declared in a scope dies when it goes out of scope. This means that the object cannot be accessed in a nesting scope (block).

Do vectors have bounds checking?

Since a vector can have any arbitrary size, there is no way to check the bounds at compile time. No. Compile time bound checking is impossible in this case, because the size of the vector is variable.

What happens when a vector is resized?

vector::resize() The function alters the container's content in actual by inserting or deleting the elements from it. It happens so, If the given value of n is less than the size at present then extra elements are demolished.


2 Answers

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.

like image 65
kgraney Avatar answered Oct 13 '22 07:10

kgraney


As stated in kgraney's answer, this is undefined behaviour. However, most c++ libraries have some facility to abort, or raise an exception in such cases. Usually controlled by setting or unsetting specific compiler macro's.

I have made an overview of the relevant documentation:

gnu libstdc++

  • Debug mode -- general info about libstdc++ debugging
  • _GLIBCXX_DEBUG
  • _GLIBCXX_CONCEPT_CHECKS, with -fconcepts -- enable c++ concepts

clang libcxx

  • _LIBCPP_DEBUG_LEVEL=1

boost

  • BOOST_DISABLE_ASSERTS -- disable asserts in the boost library.

Microsoft

  • Checked Iterators
  • _ITERATOR_DEBUG_LEVEL -- set iterator debug level
  • Security Features in the CRT
  • _CRT_SECURE_NO_WARNINGS : disable deprecation warnings
  • _SCL_SECURE_NO_WARNINGS -- less safe(according to microsoft), but more standard compliant:

  • _SECURE_SCL -- old method of setting iterator debug level

  • _HAS_ITERATOR_DEBUGGING - deprecated macro

Note that gnu and clang disable the checks by default, while microsoft has them enabled by default. If you are unaware of this, your code may run significantly slower in debug mode on a microsoft system.

like image 39
Willem Hengeveld Avatar answered Oct 13 '22 06:10

Willem Hengeveld