简体   繁体   English

无法调试此代码的分段错误

[英]unable to debug segmentation fault for this code

This is the problem from online hackerrank online platform.这是来自在线hackerrank在线平台的问题。 Nikita and the Game 尼基塔和游戏

I coded this and my solution passes only the given testcases, it is giving segmentation fault for other testcases on submit.我对此进行了编码,并且我的解决方案仅通过了给定的测试用例,它在提交时为其他测试用例提供了分段错误。
I compared my approach with editorial solutions, and one of the solution uses same approach as mine( the binary search approach to find the index at which we'll be able to split the array ).我将我的方法与编辑解决方案进行了比较,其中一个解决方案使用了与我相同的方法(二进制搜索方法来查找我们能够拆分数组的索引)。

But i can't figure out why is it giving the segmentation fault.但我不明白为什么会出现分段错误。

#include <bits/stdc++.h>
#define lld long long

using namespace std;


lld isPossible(vector<lld>&pre_sum, lld start, lld end){
    
    lld low = start-1;
    lld high = end;
    
    while(start<=end){
        lld mid = (start+end)/2;
        lld left_sum = pre_sum[mid] - pre_sum[low];
        lld right_sum = pre_sum[high]- pre_sum[mid];
        
        if(left_sum == right_sum) return mid;
        if(left_sum < right_sum) start = mid+1;
        if(left_sum > right_sum) end = mid-1;
    }
    return -1;
}

lld calcAns(vector<lld>&pre_sum, lld start, lld end){
    //cout<<start<<" "<<end<<"\n";
    lld idx = isPossible(pre_sum, start, end);
    //cout<<idx<<"\n";
    if(idx == -1) return 0;
    return 1 + max(calcAns(pre_sum, start, idx), calcAns(pre_sum, idx+1, end));
    
}
/*
 * Complete the arraySplitting function below.
 */
lld arraySplitting(vector<lld> arr) {
    
    vector<lld> pre_sum(arr.size() + 1);
    pre_sum[0] = 0;
    
    for(lld i=0; i<arr.size(); i++){
        pre_sum[i+1] = pre_sum[i] + arr[i]; 
    }
    
    lld ans = calcAns(pre_sum, 1, pre_sum.size()-1);
    //cout<<ans<<"\n";
    return ans;
}

int main()
{

    lld t;
    cin >> t;

    for (lld t_itr = 0; t_itr < t; t_itr++) {
        lld arr_count;
        cin >> arr_count;

        vector<lld> arr(arr_count);

        for (lld arr_itr = 0; arr_itr < arr_count; arr_itr++) {
            lld arr_item;
            cin>>arr_item;
            arr[arr_itr] = arr_item;
        }

        lld result = arraySplitting(arr);

        cout << result << "\n";
    }

    return 0;
}

Explanation of code:代码说明:

isPossible function finds the index at which the sum of the left part [start....mid] of the array is equal to the right part [mid+1.....end] . isPossible函数查找数组左侧部分[start....mid]的总和等于右侧部分[mid+1.....end]的索引。

calcAns calculates the no of possible such indexes in a way that once it will finds such index, it will ignore one part and finds in other part if any such possible. calcAns以一种方式计算可能的此类索引的数量,一旦找到此类索引,它将忽略一个部分,并在可能的情况下查找另一部分。 It will calculate maximum no.它将计算最大数量。 of such possible splittings.这种可能的分裂。

arraySplitting just calculates the prefix sum array pre_sum and is one size more than the actual array, ie first index have sum zero, so that it will ease calculations later while calculating sums between a range. arraySplitting只计算前缀和数组pre_sum并且比实际数组大一个大小,即第一个索引的总和为零,以便稍后在计算范围之间的总和时简化计算。 It calls calcAns function for further processing.它调用calcAns函数进行进一步处理。

EDIT: i want to learn debugging cases without actually having to use debugger as testcases are not downloadable during a contest submission.编辑:我想学习调试案例,而实际上不必使用调试器,因为在比赛提交期间无法下载测试案例。 so i want to learn what thing's am i missing which causes segmentation faults as i started training myself for programming contests.所以我想知道我错过了什么导致分段错误,因为我开始为编程比赛训练自己。 If someone can predict on seeing the code that these type of tescases is possible where my code probably can give segmentation fault, will be helpful.如果有人可以在看到代码时预测这些类型的测试用例是可能的,而我的代码可能会出现分段错误,这将很有帮助。 This is what i'm actually looking for, otherwise it is not any benefit that i debug large testcases by using gdb on setting breakpoints after the contest.这就是我真正要寻找的,否则我在比赛后通过使用 gdb 设置断点来调试大型测试用例没有任何好处。

The first step to fixing the issue is to determine where the segfault is occurring.解决问题的第一步是确定发生段错误的位置。

You can debug the program using gdb, step, set breakpoints etc.您可以使用 gdb、step、设置断点等来调试程序。

Also add debug-couts as suggested in the comments.还要按照评论中的建议添加 debug-couts。

You could add defensive checks to the methods, eg compare the indices low , mid , high against the size of the vector.您可以为这些方法添加防御性检查,例如将索引lowmidhigh与向量的大小进行比较。 It's also possible that the vector capacity setting could cause an out-of-memory exception so you should add try-except handlers around the code as well.向量容量设置也可能导致内存不足异常,因此您还应该在代码周围添加 try-except 处理程序。

Alternatively you can use a library that catches the segfault and provides a stack trace so you can see where the segfault originates, eg https://github.com/certik/stacktrace或者,您可以使用捕获段错误并提供堆栈跟踪的库,以便您可以查看段错误的来源,例如https://github.com/certik/stacktrace

Also see related questions such as how to debug c++ runtime errors另请参阅相关问题,例如如何调试 c++ 运行时错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM