Accessing an out-of-bounds index

suggest change

It is undefined behavior to access an index that is out of bounds for an array (or standard library container for that matter, as they are all implemented using a raw array):

int array[] = {1, 2, 3, 4, 5};
array[5] = 0;  // Undefined behavior

It is allowed to have a pointer pointing to the end of the array (in this case array + 5), you just can’t dereference it, as it is not a valid element.

const int *end = array + 5;  // Pointer to one past the last index
for (int *p = array; p != end; ++p)
  // Do something with `p`

In general, you’re not allowed to create an out-of-bounds pointer. A pointer must point to an element within the array, or one past the end.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Undefined behavior:
* Accessing an out-of-bounds index

Table Of Contents
8 Arrays
11 Loops
39 Streams
51 Unions
56 Lambdas
60 SFINAE
62 RAII
67 Sorting
71 Undefined behavior
84 RTTI
87 Scopes
104 Profiling
107 Recursion
117 Iteration
125 Alignment
134 Semaphore
136 Debugging
139 Mutexes
142 decltype