-4

Suppose my vector size is defined as 6.

vector<int>v(6);

But when I output , it doesn't give me error.

cout<<v[7]; //works

I know how vector actually grow , they will predefine some size x and the double it. So it can be acceptable for position 7.

But it still works for position 10000 why? Why can I access a memory location so far which I haven't declared?

cout<<v[10000]; //works
Aman Jain
  • 3
  • 3
  • 1
    It's just bad luck. Indexing out of range produces undefined behavior. Sometimes the result is that it acts exactly the way you expect it to. Until it doesn't. – Pete Becker Jul 04 '21 at 14:39
  • @PeteBecker so can I say it would work sometimes for small values , but otherwise it should give undefined behaviour – Aman Jain Jul 04 '21 at 14:42
  • No, it **always** produces undefined behavior. But sometimes undefined can look like it's working even if it isn't. – Mark Ransom Jul 04 '21 at 14:43
  • @RaymondChen hi , my question is not how to do boundry checks , I just want to know can I do like `v[10000000]` even if my size is `v(6)` ? – Aman Jain Jul 04 '21 at 14:44
  • @MarkRansom " But sometimes undefined can look like it's working even if it isn't" how and why? This is exactly my question – Aman Jain Jul 04 '21 at 14:46
  • 1
    I don't think it will destroy the site, quite the opposite. SO requires askers to do a modicum of work on their own, and this question has already been asked **and** answered many many many times on this site. – sweenish Jul 04 '21 at 14:46
  • But that is exactly the point. Without boundary checks, C++ is not required to tell you when it isn't working. – Mark Ransom Jul 04 '21 at 14:46
  • Have you looked up the literal meaning of the word "undefined"? Your question is impossible to answer. – Mark Ransom Jul 04 '21 at 14:48
  • @MarkRansom can we have a discussion in some room. I want to talk about one bug which I figured out in leetcode with your solution – Aman Jain Jul 04 '21 at 14:50
  • @AmanJain I don't think there is anything more to discuss. – Mark Ransom Jul 04 '21 at 14:55
  • @AmanJain - Saying you haven't done enough work to find an answer before asking your question isn't rude. I used a couple of key words in your subject line and body of your question, and found a number of duplicates. – Peter Jul 04 '21 at 14:58
  • 1
    The error is the assume that since you didn't see any problem then it must have worked. It looks like you got unlucky and the program didn't crash. Instead, it probably corrupted something else, elsewhere in the program and it will instead crash or give incorrect results later on. Then, you will be stuck debugging problems in an unrelated part of your program which doesn't actually contain any errors. A lot of the time C++ does not require the compiler to tell you about every type of error, and doesn't require obvious diagnosable behavior when there is an error at runtime. – François Andrieux Jul 04 '21 at 15:01
  • 1
    @Peter to be fair, it is hard for newbies to find duplicates because they don't have enough vocabulary to find the right "magic keywords" to dig up the appropriate duplicate. I've been working with C++ for decades, and even I have trouble finding good duplicates. – Raymond Chen Jul 04 '21 at 15:19
  • @RaymondChen yes very true! – Aman Jain Jul 04 '21 at 15:33
  • It was specifically stated that they searched using keywords from the OP's question. The vocabulary argument is moot in this instance. – sweenish Jul 05 '21 at 03:30

1 Answers1

1

The answer can be read in the C++ reference.

Here you can read that no boundary check will be done and no new element will be inserted. You simply have undefined behaviour.

So, in moredetail:

  • using an index operator with an index bigger than the size of the vector, will not increase the size of the vector and add new elements.
  • Using an out of bound index that is bigger than the vectors size, will simply access a memory location, which does not belong to the vector.
  • Reading such a position will mostly give a random value or better set, an undefined value. This will usually not harm your program, except that you get undefined values back.
  • Writing to an out of bounds value usually is a very severe problem, because you are overwriting memory, which you do not won. Very dangerous.

If you want to have boundary checking, then use vectors at-function.

A M
  • 14,694
  • 5
  • 19
  • 44
  • Sorry I didn't get it – Aman Jain Jul 04 '21 at 14:42
  • I added more expalanation. Please check – A M Jul 04 '21 at 14:51
  • Hey so I even can't do `v[10000]=1` right ? Even writing is prohibited ?/ – Aman Jain Jul 04 '21 at 14:52
  • @AmanJain I think the confusion comes from an expectation that all C++ code has defined behavior. You may have the expectation that, by just reading the code, you could always predict what will happen. But C++ (and C) have a property that is called Undefined Behavior. The language describes what will happen *as long as certain rules are followed*. If you don't follow those rules, then just about anything can happen. It may look like it works, but if you try it again later, or on another compiler, the same code won't work anymore. Your question has Undefined Behavior, it didn't obey the rules. – François Andrieux Jul 04 '21 at 14:56
  • Yes, writing is even worse. The compiler and language will allow it, the resulting program will most probably have a severe runtime problem and most probably crash – A M Jul 04 '21 at 14:57
  • @ArminMontigny unfortunately "most probably crash" is a bit too strong. It's easy to write a value to a location that isn't used for anything important, or that isn't used by a particular code path, or that just delivers incorrect results without crashing. The worst part is that the write and the side effects it causes might not be anywhere close to each other, making debugging almost impossible. – Mark Ransom Jul 04 '21 at 18:42