0

A typical declaration for an array in C/C++ is:

type name [elements];

where type is a valid type (such as int, float...), name is a valid identifier and the elements field (which is always enclosed in square brackets []), specifies the length of the array in terms of the number of elements.

So I declare an array of int have 2 elements

int a[2];
a[3] =4;

Why this not throw exception?

whoan
  • 8,143
  • 4
  • 39
  • 48
user3441187
  • 335
  • 1
  • 3
  • 6
  • the C language does not perform bounds checking – user3629249 Oct 15 '14 at 07:44
  • Possible duplicate of [Accessing an array out of bounds gives no error, why?](https://stackoverflow.com/questions/1239938/accessing-an-array-out-of-bounds-gives-no-error-why) – phuclv Jul 30 '18 at 15:24

2 Answers2

2

Out of bounds checking is something that you are probably used to from some higher level language like Java. However in C/C++ it is not done by default. It gives you a small performance hit to check the bounds of the array and therefore the C rationale is to have you do it manually in case you need it to offer the best possible performance. C++ STL containers like vector usually support an at() operation to perform bound-checking and since you can overload the []-operator you can also enable bound-checks for array-style access.

If array is a raw pointer a statement like array[i] comes down to this in C/C++:

*(array + i)

which is a simple addition of address + offset. The following statements are thus equivalent:

*(array + i), *(i + array), array[i], i[array]

What happens internally is that you take the address stored in the pointer, add i-times the size of the array type to it and then de-reference this address.

So what happens if you specify an index that is bigger than the array, is that you access memory that does not belong to the array. Effectively you read random data that is next to the array in memory. This is a typical source of buffer-overflows if the address is written to. If the memory you are trying to access does not belong to your process, you will get a segfault.

midor
  • 5,487
  • 2
  • 23
  • 52
0

'a' is simply a pointer to the first element (of that array) in memory. some compilers will allow you to access other parts of memory without giving you a warning. for proper array usage, see http://www.cplusplus.com/doc/tutorial/arrays/ i.e. use something like

#define MAX 5
int main(){
  int a[MAX];
  for(int i=0;i<MAX;i++){
     a[i]=i;
  }
}
ierdna
  • 5,753
  • 7
  • 50
  • 84
  • What happen when compiler see a[5] and what happen when we access a[6]? – user3441187 Oct 14 '14 at 15:14
  • @user3441187 - The behavior is undefined. Anything can happen -- crash, run without error, overwrite some other variable's value, etc. Unlike other programming languages, C++ does not automatically crash and give you some sort of "stack trace" when you do something wrong. – PaulMcKenzie Oct 14 '14 at 15:43
  • 1
    The compiler will consider `a` as a *pointer* and `a[5]` as `*(a + 5)`, which can be a perfectly valid address, just not in the array you defined. – Dettorer Oct 14 '14 at 15:47