1

I learned that arrays have some constant value for its size. For example;

int array[5]; //This array have only 5 index that can be usable

This is what I've learned.

But when I try this code it is not square with this hypothesis.

#include <iostream>
using namespace std;

int main()
{
    int array[4];   
    array[5]=6; 
    cout<<array[5]<<endl;   
    return 0;
}

This code works well. So I am confused.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • `int array[4]` defines an array of 4 integers, you can access them with index 0 to 3, `array[5]` is out of bound, it results undefined behavior. – fluter May 07 '16 at 09:35
  • You're writing data to the memory addresses after the end of the array. It might seem to work OK, but could very well cause a crash, or overwrite other data in memory. – GoBusto May 07 '16 at 09:35
  • "This code works well" - yes, sometimes undefined behaviour *does* work. That's why it's so insidious. – paxdiablo May 07 '16 at 09:39
  • 2
    Earlier reference from stackoverflow - [http://stackoverflow.com/questions/1239938/accessing-an-array-out-of-bounds-gives-no-error-why](http://stackoverflow.com/questions/1239938/accessing-an-array-out-of-bounds-gives-no-error-why) – atulj May 07 '16 at 09:40
  • 1
    To enable bounds checking better use c++11 `array arr;` and than `arr.at(x)`... – W.F. May 07 '16 at 09:41
  • Or vectors, if you want resizability as well as "safety". – paxdiablo May 07 '16 at 09:43

4 Answers4

1

The array does have a constant size!

You created a 4-element array:

  • 1st element is array[0]
  • 2nd is array[1]
  • 3rd is array[2]

C++ is an unsafe language. If you do something like array[5] or even array[-1], you'll end up accessing a bunch of random data! This is called undefined behavior. C++ is free to do whatever the heck it wants when you invoke undefined behavior; your program could run perfectly fine here but crash on someone else's computer. The compiler won't check to make sure any indexes are valid first. This has nothing to do with the array having or not having a constant size.

kirbyfan64sos
  • 10,377
  • 6
  • 54
  • 75
  • 3
    C++ (and C) are only "unsafe" for people who don't know what they're doing. Chainsaws, for example, are *very* safe if you know how to use them and I wouldn't go back to using a handsaw for big jobs since it's too much like hard work :-) – paxdiablo May 07 '16 at 09:41
0

Undefined Behavior is the answer.

C++ doesn't do runtime's check on bound. So in your case it works but you're not using array space. In other situation you will get an exception. Moreover in other situation you can use array[10] but you are using other space gave to the program by your OS.

iFarbod
  • 639
  • 11
  • 26
granmirupa
  • 2,780
  • 16
  • 27
0

It works, because memory after reaching end of array variable exists. The problem is that it is not reserved for this array. It means that array[5] will point to a memory tile, but you cannot be sure what to expect from it. It may be other variable, it may be reserved by another program. If you manage to edit it (like you did) problem will come when other variable or program will reserve this memory position. Then your array[5] will no longer hold value 6, but it will change (and it even may become unreachable from your working code).

Łukasz Szcześniak
  • 1,417
  • 11
  • 23
0

Declaring an array like int ar[n] allows you to use indices from 0 to n-1. accessing any index beyond this range is going to give you unexpected behaviour.

awesome_me
  • 41
  • 4