1

consider this definition

int a[3][3] = {0};  

Here i have 9 allocated contiguous memory spaces with the total size of 3*3*sizeof(int)
bytes stored for the array. Now i have this assignment

a[0][12] = 3;  

After reading this post, i understood that i am accessing the a[12] address from the base address.
But now my question is how is a[0][12] array subscript able to access a value that is out of the range assigned to a??

Community
  • 1
  • 1
Samuel
  • 612
  • 2
  • 9
  • 25
  • Related to [Is accessing a global array outside its bound undefined behavior?](http://stackoverflow.com/q/26426910/1708801) – Shafik Yaghmour Feb 03 '15 at 16:41
  • C++ not only lets you "shoot yourself in the foot" it also hands you the gun. Range checking (of array indexes) falls into the "don't pay for what you don't need" guide lines and is therefore not provided. – Richard Critten Feb 03 '15 at 16:47

1 Answers1

2

It's undefined behaviour because you're accessing memory from some memory block.

So it will most probably return what's at this position in your memory but it's undefined.

Or if you assign something to the memory position, you're corrupting your own memory which can lead to all sorts of issues if it works at all.

deW1
  • 5,562
  • 10
  • 38
  • 54