29

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
}
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
Shibli
  • 5,879
  • 13
  • 62
  • 126
  • 2
    size 6, so last is `vec[5]` starts with 0 – Bill May 18 '13 at 02:56
  • 1
    Not sure where you got the idea that `vector` would throw an error on an invalid index using `operator[]`. The docs certainly say otherwise. – Ed S. May 18 '13 at 02:57
  • `Notice that the first element has a position of 0 (not 1).` -- http://www.cplusplus.com/reference/vector/vector/operator[]/ – Bill May 18 '13 at 02:58
  • @Bill: I think they understand that indexes start at zero and that 6 is out of range, but they're wondering why accessing something out of range does not yield an error. – icktoofay May 18 '13 at 02:59
  • @EdS. are there any reasons that there is no bounds checking implemented for op `[]`? – Koushik Shetty May 18 '13 at 03:00
  • @icktoofay: All one needs to do is read the docs to see that it results in UB – Ed S. May 18 '13 at 03:00
  • 3
    @Koushik: Because it slows things down. That's what `at()` is for – Ed S. May 18 '13 at 03:00
  • `If the container size is greater than n, the function never throws exceptions (no-throw guarantee). Otherwise, the behavior is undefined.` -- same link posted in my earlier comment. – Bill May 18 '13 at 03:02
  • @EdS.: I know, and that's what my answer states. I'm not wondering, the person that asked the question is! – icktoofay May 18 '13 at 03:02
  • 2
    11 upvotes for simply failing to consult the documentation. Why? – Lightness Races in Orbit Feb 16 '18 at 01:57

4 Answers4

58

std::vector performs bounds checking when the at() member function is used, but it does not perform any checks with operator[].

When out of bounds operator[] produces undefined results.

François Andrieux
  • 28,148
  • 6
  • 56
  • 87
kgraney
  • 1,975
  • 16
  • 20
  • 12
    The behavior is undefined, so implementations are **allowed** to introduce bounds checks; the difference from `at` is that bounds checks are **required** for `at`. – Pete Becker May 18 '13 at 15:46
12

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++

clang libcxx

boost

Microsoft

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.

Willem Hengeveld
  • 2,758
  • 23
  • 19
4

It's undefined behavior. Undefined behavior does not necessarily mean you'll get an error: you might, but you might instead get some result that doesn't make much sense.

icktoofay
  • 126,289
  • 21
  • 250
  • 231
-2

Data structures are indexed starting at 0, so if you are accessing vec[6] then this is going to be out of bounds. You are likely not getting an error due to a memory issue; there could be something there from previous code you have run, or some similar error. Please post code.

john smith
  • 189
  • 1
  • 3
  • 14