In this article, we will discuss the reason for the run-time error and its solution.
Method 1: When the index of the array is assigned with a negative index it leads to invalid memory access during runtime error. Below is the C++ Program to illustrate the invalid memory access during run-time:
- This is an error for index out of bound.
- It can be resolved by using the size of the array/vector as within the limit.
Method 3: Some silly mistakes encountered while coding in hurry, sometimes leads to runtime error. Below is the C++ program illustrating runtime error by un-assigned variables:
The above program shows “Bad memory access (SIGBUS)” because:
- Here, variable N is assigned a garbage value resulting in a runtime error.
- Sometimes, since it depends on the compiler how it assigned the garbage value.
- This can be resolved by declaring arr[N] after scanning the value for variable n and checking if it is the upper or lower limit of the array/vector index.