You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

程序本地与IDE运行正常但提交触发段错误(SIGSEGV)的调试求助

Debugging Segmentation Fault in GeeksforGeeks Bike Race Problem C++ Code

Problem Statement:
A bike race is organized with N riders. The i-th rider has an initial speed (H_i) and acceleration (A_i) km/h. For safety, the total track speed is monitored every hour: riders with speed ≥ L are considered fast riders, and the total track speed is the sum of current speeds of all fast riders. We need to find the minimum number of hours when the total speed reaches or exceeds M km/h, triggering the safety alarm.

Constraints:
1 ≤ T ≤ 100, 1 ≤ N ≤ 1e5, 1 ≤ M,L ≤ 1e10, 1 ≤ H_i,A_i ≤ 1e9

Your Code

using namespace std;
#include<iostream>

class biker {
public:
    int initspeed, acc, speeding;
};

void input(int n, biker a[]) {
    for (int i = 0; i < n; i++) {
        //cout<<"Enter the initspeed:"<<"\n";
        std::cin >> a[i].initspeed;
        //cout<<"Enter the acceleration:"<<"\n";
        std::cin >> a[i].acc;
    }
}

int main() {
    int n = 0, i = 0, t = 0, max_speed = 0, flag = 0, minimum = 0;
    biker a[100];
    std::cin >> t;
    for (int k = 0; k < t; k++) {
        //cout<<"Enter no of bikers"<<"\n";
        std::cin >> n;
        //cout<<"enter the max track speed"<<"\n";
        std::cin >> max_speed;
        //cout<<"Enter the min niker speed"<<"\n";
        std::cin >> minimum;
        input(n, a);
        int j = 1, x = 0;
        while (flag < 500) {
            int sum = 0;
            for (i = 0; i < n; i++) {
                int prod = 0;
                prod = a[i].acc * j;
                x = a[i].initspeed + prod;
                // cout<<"VAL"<<x<<"\n";
                if (x >= minimum) {
                    //cout<<"It is greater than minimum";
                    sum = sum + a[i].initspeed + prod;
                    //cout<<sum<<"\n";
                }
            }
            if (sum >= max_speed) {
                //cout<<"MAXIMUM ACHIEVED\n";
                x = sum;
                break;
            }
            j++;
            flag++;
        }
        //cout<<x<<"\n";
        std::cout << j;
    }
    return 0;
}

Issue Encountered

Your code runs fine locally in IDEs, but throws a Segmentation Fault (SIGSEGV) when submitted to GeeksforGeeks. Let's walk through the most likely causes and how to fix them:

Debugging & Fix Steps

  1. Fixed-Size Array Overflow (Primary Cause)

    • Looking at your main() function, you declared a fixed array biker a[100];, but the problem allows (N) up to 1e5. When the test case has more than 100 riders, your code writes way beyond the array's bounds—this is the classic trigger for a segmentation fault.
    • Local vs Online Behavior: You probably tested with small (N) values (<=100) locally, so the overflow didn't crash. Online judges use large test cases that hit this limit immediately.
    • Fix: Replace the fixed array with a dynamic container like std::vector<biker>. For each test case, initialize it with size (n):
      // In main(), replace biker a[100]; with:
      std::vector<biker> a;
      // Then inside the test case loop:
      cin >> n;
      a.resize(n); // Allocate space for n bikers
      
  2. Integer Overflow Leading to Undefined Behavior

    • All your speed/acceleration/sum variables are int types, which can only hold up to ~2e9 (for 32-bit ints). But per constraints, (H_i) and (A_i) can be 1e9, multiplying by even 500 gives 5e11—way beyond the int limit. Overflow leads to undefined behavior, which can manifest as segmentation faults or incorrect calculations.
    • Fix: Switch all relevant variables to 64-bit integers (long long):
      • Update the biker class: long long initspeed, acc;
      • Change max_speed, minimum, sum, prod, x to long long in main() and input().
  3. Uninitialized flag Reset Between Test Cases

    • You declared flag once at the top of main(), but never reset it between test cases. After the first test run, flag will be >=500, so all subsequent test cases skip the while loop entirely. While this might not directly cause a segfault, it leads to wrong output and can interact with other issues.
    • Fix: Move int flag = 0; inside the test case loop (right before the while loop) so it resets to 0 for each test.
  4. Inefficient Linear Search (Bonus Optimization)

    • Your current approach increments j one by one, which will be way too slow for large required times (like 1e10 hours). A binary search approach is necessary to pass all test cases:
      • Define a low (0) and high (a large upper bound like 1e18) for the time.
      • For each mid value, calculate the total sum of speeds >= L. If sum >= M, try to find a smaller time; else, increase the time.
      • This reduces the number of iterations from thousands to ~60, which is feasible for large inputs.
  5. Validate with Debugging Tools

    • If you can replicate the segfault locally, use gdb to get a stack trace:
      • Compile with -g flag: g++ -g your_code.cpp -o bike_race
      • Run gdb ./bike_race, then use run with a test case that has (N=1e5). The bt command will show exactly where the crash occurs, confirming the array overflow.

内容的提问来源于stack exchange,提问作者genuineGenius

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:45:44