程序本地与IDE运行正常但提交触发段错误(SIGSEGV)的调试求助
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
Fixed-Size Array Overflow (Primary Cause)
- Looking at your
main()function, you declared a fixed arraybiker 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
- Looking at your
Integer Overflow Leading to Undefined Behavior
- All your speed/acceleration/sum variables are
inttypes, 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 theintlimit. 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
bikerclass:long long initspeed, acc; - Change
max_speed,minimum,sum,prod,xtolong longinmain()andinput().
- Update the
- All your speed/acceleration/sum variables are
Uninitialized
flagReset Between Test Cases- You declared
flagonce at the top ofmain(), but never reset it between test cases. After the first test run,flagwill 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.
- You declared
Inefficient Linear Search (Bonus Optimization)
- Your current approach increments
jone 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.
- Your current approach increments
Validate with Debugging Tools
- If you can replicate the segfault locally, use
gdbto get a stack trace:- Compile with
-gflag:g++ -g your_code.cpp -o bike_race - Run
gdb ./bike_race, then userunwith a test case that has (N=1e5). Thebtcommand will show exactly where the crash occurs, confirming the array overflow.
- Compile with
- If you can replicate the segfault locally, use
内容的提问来源于stack exchange,提问作者genuineGenius

