Accessing array out of bounds in C/C++ Last Updated : 20 Jan, 2025 Comments Improve Suggest changes Like Article Like Report Prerequisite: Arrays in C/C++In high level languages such as Java, there are functions which prevent you from accessing array out of bound by generating a exception such as java.lang.ArrayIndexOutOfBoundsException. But in case of C, there is no such functionality, so programmer need to take care of this situation. What if programmer accidentally accesses any index of array which is out of bound?C don't provide any specification which deal with problem of accessing invalid index. As per ISO C standard it is called Undefined Undefined Behaviour. An undefined behavior (UB) is a result of executing computer code whose behavior is not prescribed by the language specification to which the code can adhere to, for the current state of the program (e.g. memory). This generally happens when the translator of the source code makes certain assumptions, but these assumptions are not satisfied during execution. Examples of Undefined Behavior while accessing array out of bounds1. Access non allocated location of memory: The program can access some piece of memory which is owned by it. C // Program to demonstrate // accessing array out of bounds #include <stdio.h> int main() { int arr[] = {1,2,3,4,5}; printf("arr [0] is %d\n", arr[0]); // arr[10] is out of bound printf("arr[10] is %d\n", arr[10]); return 0; } Output : arr [0] is 1arr[10] is -1786647872 It can be observed here, that arr[10] is accessing a memory location containing a garbage value.2. Segmentation fault: The program can access some piece of memory which is not owned by it, which can cause crashing of program such as segmentation fault. C // Program to demonstrate // accessing array out of bounds #include <stdio.h> int main() { int arr[] = {1,2,3,4,5}; printf("arr [0] is %d\n",arr[0]); printf("arr[10] is %d\n",arr[10]); // allocation memory to out of bound // element arr[10] = 11; printf("arr[10] is %d\n",arr[10]); return 0; } OutputRuntime Error : Segmentation Fault (SIGSEGV) Important Points:Stay inside the bounds of the array in C programming while using arrays to avoid any such errors.C++ however offers the std::vector class template, which does not require to perform bounds checking. A vector also has the std::at() member function which can perform bounds-checking. Comment More infoAdvertise with us Next Article Accessing array out of bounds in C/C++ M Mandeep Singh Improve Article Tags : Misc Technical Scripter C Language C++ c-array cpp-array +2 More Practice Tags : CPPMisc Similar Reads Pointer to an Array in C++ Pointers in C++ are variables that store the address of another variable while arrays are the data structure that stores the data in contiguous memory locations. In C++, we can manipulate arrays by using pointers to them. These kinds of pointers that point to the arrays are called array pointers or 6 min read Arrays and Strings in C++ Arrays An array in C or C++ is a collection of items stored at contiguous memory locations and elements can be accessed randomly using indices of an array. They are used to store similar types of elements as in the data type must be the same for all elements. They can be used to store the collection 5 min read array::begin() and array::end() in C++ STL Array classes are generally more efficient, light-weight and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::begin() begin() function is used to return an iterator pointing to the first element of the array containe 3 min read Properties of Array in C An array in C is a fixed-size homogeneous collection of elements stored at a contiguous memory location. It is a derived data type in C that can store elements of different data types such as int, char, struct, etc. It is one of the most popular data types widely used by programmers to solve differe 8 min read Maximum Size of an Array in C Array in C is a collection of elements of the same data type that are stored in contiguous memory locations. The size of an array is determined by the number of elements it can store and there is a limit on this size. The maximum size of an array in C is determined by many factors, including the dat 4 min read array::front() and array::back() in C++ STL Array classes are generally more efficient, light-weight, and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::front() This function is used to reference the first element of the array container. This function can be 3 min read Calculating the address of an element in an N-dimensional array N-Dimensional Arrays: The N-Dimensional array is basically an array of arrays. As 1-D arrays are identified as a single index, 2-D arrays are identified using two indices, similarly, N-Dimensional arrays are identified using N indices. A multi-dimensional array is declared as follows: int NDA[S1][S2 4 min read Array of Vectors in C++ STL Prerequisite: Arrays in C++, Vector in C++ STL An array is a collection of items stored at contiguous memory locations. It is to store multiple items of the same type together. This makes it easier to get access to the elements stored in it by the position of each element. Vectors are known as dynam 3 min read 4 Dimensional Array in C/C++ Prerequisite :Array in C/C++, More on array A four-dimensional (4D) array is an array of array of arrays of arrays or in other words 4D array is a array of 3D array. More dimensions in an array means more data be held, but also means greater difficulty in managing and understanding arrays. Declarati 3 min read STD::array in C++ The array is a collection of homogeneous objects and this array container is defined for constant size arrays or (static size). This container wraps around fixed-size arrays and the information of its size are not lost when declared to a pointer. In order to utilize arrays, we need to include the ar 5 min read Like