Reason of runtime error in C/C++ Last Updated : 20 Dec, 2022 Comments Improve Suggest changes Like Article Like Report In this article, we will discuss the reason for the run-time error and its solution. Runtime Error: A runtime error in a program is an error that occurs while the program is running after being successfully compiled. Below are some methods to identify the reason behind Runtime errors:Â 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:Â C++ // C++ program to illustrate invalid // memory access during run-time #include <iostream> using namespace std; // Global declaration int arr[5]; // Driver Code int main() { int answer = arr[-10]; cout << answer; return 0; } Output:1736487104 Method 2: Sometimes Array or vector runs out of bounds of their limits resulting in a runtime error. Below is the C++ program illustrating array runs out of bound: C++ // C++ program to illustrate // array runs out of bound #include <iostream> using namespace std; // Driver Code int main() { long n; n = 100000000000; // 'n' is out of bound for // the array limit long a[n]; cout << a[1] << " "; return 0; } Output: Explanation: 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 like dividing by zero encountered while coding in a hurry, sometimes leads to runtime error. Below is the C++ program illustrating runtime error by dividing by zero and un-assigned variables: C++ #include <iostream> using namespace std; int main() { int n = 0; cout << 5/n; return 0; } Output:Â Â C++ // C++ program to illustrate runtime // error by un-assigned variables #include <iostream> using namespace std; // Driver Code int main() { long long N; // N is assigned garbage value long arr[N]; cin >> N; for (int i = 0; i < N; i++) { cin >> arr[i]; } for (int i = 0; i < N; i++) { cout << arr[i] << " "; } return 0; } Output: Explanation: The above program shows "Bad memory access (SIGBUS)" because: Here, variable N is assigned a garbage value resulting in a runtime error.Sometimes, it depends on how the compiler 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. Comment More infoAdvertise with us Next Article Reason of runtime error in C/C++ H harishsinghbohra Follow Improve Article Tags : Competitive Programming C++ Programs C++ DSA CPP-Basics +1 More Practice Tags : CPP Similar Reads regex_error in C++ regex_error is present inside the Header "regex" and inside the Class regex_error;. It helps us to know about the errors which are thrown during the program execution, it defines the type of the object of exception in regular expressions library, Also describes an error in the construction or use of 2 min read Syntax Error in C++ Syntax plays a vital role and even a slight mistake can cause a lot of unexpected errors. One of these errors is the "expected unqualified id error" or "expected unqualified id before ' ' token" which can arise due to some common oversights while writing your code. In this article, we are going to d 6 min read Errors in C/C++ Error is an illegal operation performed by the user which results in abnormal working of the program. Programming errors often remain undetected until the program is compiled or executed. Some of the errors inhibit the program from getting compiled or executed. Thus errors should be removed before c 5 min read How to find Segmentation Error in C & C++ ? (Using GDB) What is Segmentation Error ? - It is the runtime error caused because of the memory access violation. For Eg :-Stackoverflow, read violation etc.. We often face this problem when working out with pointers in c++/c. In this example we will see how to find the segmentation error in the program. We wil 3 min read C++ Tutorial | Learn C++ Programming C++ is a popular programming language that was developed as an extension of the C programming language to include OOPs programming paradigm. Since then, it has become foundation of many modern technologies like game engines, web browsers, operating systems, financial systems, etc.Features of C++Why 5 min read Like