
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Accessing array out of bounds in C/C++
An array in C/C++ is a fixed-size sequential collection of elements of the same data type where all the elements are stored in the contiguous memory allocation. If an array is accessed out of bounds then an undefined behavior will occur in C/C++, unlike Java where an exception such as java.lang.ArrayIndexOutOfBoundsException will occur.
Accessing Out of Bound Memory
Accessing out-of-bounds memory in an array means we are trying to access the array index outside its valid range size (i.e., index <0 and index >= array size). It returns any garbage value in the output.
Example
In this example, an array of 5 elements is initialized and we are trying to access 7 elements. In this case, it will print the first 5 array elements, and after that, it will print garbage values.
#include<stdio.h> int main() { int arr[] = {1, 2, 3, 4, 5}; printf("The elements of array : "); for (int i = 0; i < 7; i++) printf(" %d", arr[i]); return 0; }
The output of the above code is as follows:
The elements of array : 1 2 3 4 5 32765
#include <iostream> using namespace std; int main() { int arr[] = {1, 2, 3, 4, 5}; cout << "The elements of array : "; for (int i = 0; i < 7; i++) cout << arr[i] << " "; return 0; }
The output of the above code is as follows:
The elements of array : 1 2 3 4 5 0 -724676608
Out of Bounds Array Element Assignment
When we try to assign and access an array element at an index outside its range, it returns a segmentation fault.
Example
Here is an example of assigning an element at the 10th index when the array size is 5:
#include<stdio.h> int main() { int arr[] = {1, 2, 3, 4, 5}; arr[10] = 57; printf(" %d", arr[10]); return 0; }
The output of the above code is as follows:
Segmentation fault (core dumped)
#include <iostream> using namespace std; int main() { int arr[] = {1, 2, 3, 4, 5}; arr[10] = 57; cout << arr[10]; return 0; }
The output of the above code is as follows:
Segmentation fault (core dumped)
Accessing Out of Bound Memory Using vector
A vector in C++ is a dynamic array that stores a collection of elements of the same data type and it can be automatically resized. When we try to access out-of-bound memory in vector then it throws an error out_of_range instead of showing undefined behavior like giving garbage value.
Example
In this example, we have a vector having five elements, and we are trying to print seven elements. This will throw an error as we are trying to access elements outside of valid vector size.
#include <vector> #include <iostream> using namespace std; int main() { vector<int> v = {1, 2, 3, 4, 5}; for (size_t i = 0; i < 7; ++i) { cout << v.at(i) << " "; } cout << endl; return 0; }
The output of the above code will be an error that is shown below in the output:
terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check: __n (which is 5) >= this->size() (which is 5) Aborted
Conclusion
In this article, we discussed about the undefined behavior while accessing array out of bounds in C/C++. The undefined behavior may be a garbage value or segmentation fault when an array is accessed or assigned outside its size range respectively.