简体   繁体   English

分段错误:无序集插入时为 11

[英]Segmentation fault: 11 on unordered set insertion

I can't get why is it showing segmentation error with these given values of numbers and target我不明白为什么这些给定的数字和目标值会显示分段错误

int findind(vector<int> s,int tar, int high , int low ){
    int mid = (high - low)/2;
    if(s.at(mid) == tar){
        return mid;
    }
    else if (tar < s.at(mid)){
        high = mid;
        return findind(s,tar,high,low);
    }
    else{
        low = mid;
        return findind(s,tar,high,low);
    }
}

int main(){
    vector<int> numbers = {-1,0,1,4,5,7};
    int target = 8;
    int id1=-1, id2=-1, N = numbers.size();
    unordered_set<int> us;
    vector<int> ids;
    for (int i = 0; i < N; ++i)
    {   if (numbers[i] == target/2 && target%2 == 0){
            if (i!=N-1 && numbers[i+1] == numbers[i])
            {
                id1 = i;
                id2 = i+1;
                break;
            }
            else{
                us.insert(numbers[i]);
            }
        }

        else if(us.find(target - numbers[i]) == us.end()){
            us.insert(numbers[i]);
        }
        else{
            id1 = i;
            id2 = findind(numbers,target - numbers[i],N,0);
            break;
        }
    }
    if(id1 < id2 ){
        ids.push_back(id1+1);
        ids.push_back(id2+1);
    }
    else{
        ids.push_back(id2+1);
        ids.push_back(id1+1);
    }
    cout<<ids[0]<<" "<<ids[1]<<" ";
    return 0; 
}

error in sublime text is :崇高文本中的错误是:

enter code here line 1: 67625 Segmentation fault: 11 gtimeout 4s ./TwoSumII-InputArrayIsSorted < inputf.in > outputf.in [Finished in 1.4s with exit code 139] enter code here第 1 行:67625 分段错误:11 gtimeout 4s ./TwoSumII-InputArrayIsSorted < inputf.in > outputf.in [在 1.4s 内完成,退出代码 139]

and error in leetcode is : leetcode 中的错误是:

AddressSanitizer:DEADLYSIGNAL
=================================================================
==30==ERROR: AddressSanitizer: stack-overflow on address 0x7ffd7e8e8fd8 (pc 0x000000334f91 bp 0x00000000001c sp 0x7ffd7e8e8fd0 T0)
==30==ABORTING

also if I put numbers = {3,24,50,79,88,150,345} and target = 200, same error occurs.另外,如果我输入 numbers = {3,24,50,79,88,150,345} 和 target = 200,也会发生同样的错误。

But if I comment out us.insert(numbers[i]) in :但是,如果我在以下位置注释掉 us.insert(numbers[i]) :

else if(us.find(target - numbers[i]) == us.end()){
            us.insert(numbers[i]);
}

and put cout<<i<<" ";并把 cout<<i<<" "; then it don't give this error那么它不会给出这个错误

I am not clear on what you intend by your main function, but certainly a big part of the problem is that findind will run forever if tar does not match any value in s because the only way out of the function is if it finds a match, satisfying the condition (s.at(mid) == tar) .我不清楚你的main函数的意图,但问题的很大一部分是如果tars中的任何值都不匹配, findind将永远运行,因为退出函数的唯一方法是找到匹配项,满足条件(s.at(mid) == tar) Otherwise it will recurse, adding another entry on the stack and eventually the stack will overflow.否则它将递归,在堆栈上添加另一个条目,最终堆栈将溢出。

int findind(vector<int> s,int tar, int high , int low ){
    int mid = (high - low)/2;
    if(s.at(mid) == tar){
        return mid;
    }
    else if (tar < s.at(mid)){
        high = mid;
        return findind(s,tar,high,low);
    }
    else{
        low = mid;
        return findind(s,tar,high,low);
    }
}

You need to decide what value you want findind to return if no match for tar is found in s and you should return that value if high == low .如果在s中找不到与tar的匹配项,您需要决定findind返回的值,如果high == low则应返回该值。

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

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