Invalid Memory Access During Runtime
In the below code, the array is assigned with a negative index value and this will cause invalid memory access. It will give a garbage value. Below is the C++ program to demonstrate invalid memory access during runtime:
C++
// C++ program to demonstrate // invalid memory access during // runtime #include <iostream> using namespace std; int arr[5]; // Driver code int main() { int a = arr[-10]; cout << a; return 0; } |
281923776
C++ Program to Show Runtime Exceptions
A runtime error occurs while the program is running. Because this is not a compilation error, the compilation will be completed successfully. Here, we will learn how to handle runtime exceptions in C++.
There are 5 types of runtime exceptions discussed here:
- Division by zero.
- Segmentation faults.
- Large memory allocation/Large Static Memory Allocation.
- Type Specifier Error.
- Invalid memory access during runtime.
Let’s start discussing each of these runtime errors in detail.