A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/cpp/reason-of-runtime-error-in-c-c/ below:

Reason of runtime error in C/C++

Reason of runtime error in C/C++

Last Updated : 23 Jul, 2025

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;
}

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:

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:



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4