-2

If I declare an array in C/C++ like this int arr[5]; and then a loop to insert elements in it like this

for(int i=0; i<5; i++){
    arr[i] = i+1;
}

And when I print those elements like this

for(int i=0; i<5; i++){
    printf("%d ", arr[i]);
}

elements get printed as usual i.e 1 2 3 4 5

But if those loops are run one more time like this

for(int i=0; i<=5; i++){
    arr[i] = i+1;
}

and this

for(int i=0; i<=5; i++){
    printf("%d ", arr[i]);
}

then the output comes to be 1 2 3 4 5 6.

So, I don't understand that how can an array of size 5 has 6 elements in itself?

dreamcrash
  • 47,137
  • 25
  • 94
  • 117
heikrana
  • 163
  • 2
  • 7
  • 4
    `for(int i=0; i<=5; i++){ arr[i] = i+1; }` This code exhibits undefined behavior, by way of accessing an index out of bounds. "Seems to work" is one possible manifestation of undefined behavior. – Igor Tandetnik Jan 31 '21 at 18:47
  • 3
    When I hang 1000 pounds from a rope that can only hold 900 pounds, the rope doesn't break. Why? That's basically what you're asking. It may break, it may hold up forever, it may break for your friend but not when you try it, etc. – PaulMcKenzie Jan 31 '21 at 18:47
  • But as soon as I access arr[6] it gives me an error, why not at arr[5]? – heikrana Jan 31 '21 at 18:50
  • @ornvyr -- It is pointless to reason why code that is broken acts the way it does. On another compiler, other compiler settings, etc. that code may do different things. – PaulMcKenzie Jan 31 '21 at 18:52
  • @ornvyr: You overwrite memory that belongs to something else. The more you overwrite, the larger the chance for something bad to happen. – cptFracassa Jan 31 '21 at 18:54
  • ***arr[5] = 6;*** is UB – ΦXocę 웃 Пepeúpa ツ Jan 31 '21 at 18:56
  • @ornvyr *But as soon as I access arr[6] it gives me an error, why not at arr[5]?* And when code gets more complex, you'll find out that code like that will sometimes appear to work, sometimes it will appear to fail, and sometimes code somewhere ***else*** will break because you overwrote data it needed. – Andrew Henle Jan 31 '21 at 19:16
  • C/C++ don't hold your hand. Accessing an array out of bounds doesn't necessarily cause an error. It might appear to work fine, then randomly break (or not). – HolyBlackCat Jan 31 '21 at 19:24
  • 1
    ***`(It's complicated)`*** it is actually very simple and identical quiestion is asked at least 20 times a week. Closing as a very common dupe – 0___________ Jan 31 '21 at 19:32

2 Answers2

3

That array of size 5 doesn't have 6 elements, actualy you are writing and reading from memory that does not belong to that array, you can obtain output 6 because you write to that piece of memory just before read from there. Actualy if you write that 6, do a lot of things and read that memory at the end of program there is some chances for you to read something "random" and not 6.

For a better explanation check How dangerous is it to acces an array out of bounds.

dreamcrash
  • 47,137
  • 25
  • 94
  • 117
Paul Bob
  • 441
  • 3
  • 10
3

Simply because C does not check array bounds.

Reading and writing beyond array bounds is an undefined behavior in C, and it returns an exception in other languages (IndexOutOfBoundsException in Java, for example). In your case trying to read arr[i] will just display whatever value is stored in the next 4 bytes of memory (right after the end of the array).

Check this for further explanation: No out of bounds error

Obsidian
  • 3,719
  • 8
  • 17
  • 30
hamza13
  • 146
  • 1
  • 4