-2

I'm starting to learn C++ so the answer to my question might be obvious to you but I am really puzzled. I expect the program would crash when accessing a out of range index but instead, the program runs just fine. Here is the code.

    #include <iostream>
    #include <typeinfo>
    #include <string>
    #include <vector>

    using namespace std;

    int main() {
        int x;
        vector<int> v1;

        while (cin >> x) {
            v1.push_back(x);
        }

        cout << "out of bound: " << v1[10] << endl;
    return 0;
}

And here is the output:

1 2 3
out of bound: 0
Chris N
  • 17
  • 4

1 Answers1

-3

There are 3 elements in v1, meaning there is NO v1[10], only v1[0] through v1[2].

The behavior is not, strange. That’s what is supposed to happen, because std:: vector does not perform size checking for '[]'.

So, you are basically getting a garbage from v1[10].

  • 3
    OP is well aware there `v1[10]` is out of bounds and is *not* seeing an error. That's legal, but hardly *"what is supposed to happen"*. – Baum mit Augen Feb 14 '18 at 18:14
  • I understand that there is no index v1[10] but shouldn't the program crash instead of returning 0 for non-exist index? – Chris N Feb 14 '18 at 18:15
  • @ChrisN "_shouldn't the program crash instead of returning 0_" No, it shouldn't (it can, but that is not required), who told you, that it should? – Algirdas Preidžius Feb 14 '18 at 18:15
  • 2
    @ChrisN no, it's not supposed to crash. The word "crash" doesn't even appear in the C++ standard – UnholySheep Feb 14 '18 at 18:16
  • @Baum mit Augen, I clarified and edited. Thanks. – Sajid Hasan Apon Feb 14 '18 at 18:22
  • @Chris, please see the edited answer. – Sajid Hasan Apon Feb 14 '18 at 18:22
  • Thank you for your insights. However, if I define a vector v1 without initializing it and try to access a non-exist index, the program will core dumped. I guess this has to do with 'Undefined behavior" vs something? – Chris N Feb 14 '18 at 18:22
  • @ChrisN *undefined behavior* means that *anything* can happen. From core dumps to random values to demons jumping out of your nose - all of that is legal according to the language standard – UnholySheep Feb 14 '18 at 18:24
  • @ChrisN "_I guess this has to do with 'Undefined behavior" vs something_" It is undefined behavior, as is the previous case. It may crash, or it may not - it's undefined. – Algirdas Preidžius Feb 14 '18 at 18:24
  • 2
    That's still not particularly precise in terminology. If you choose to answer fairly obvious duplicates like that one, you better be prepared for your answers being held to the same standard as the common dupe target, which is often not trivial to meet. – Baum mit Augen Feb 14 '18 at 18:25
  • @SajidHasanApon I'd recommend you take that answer off. You'll might even gain a badge for doing so. –  Feb 14 '18 at 18:38
  • @Baum mit Augen, I understand, and will be careful. Thanks. Also, I was not aware that the behavior is undefined because when I started learning STL, I too, experimented this way with various indices and did not get core dumped a single time. So, I just assumed that the value I got was from &the_vector+index*dataSize and will not get an error unless I point out of RAM. – Sajid Hasan Apon Feb 14 '18 at 18:44
  • Experiments are generally not a good way to learn C++. – Baum mit Augen Feb 14 '18 at 18:50