sort function C++ segmentation fault

被刻印的时光 ゝ 提交于 2020-07-04 07:48:40

问题


In this code, for vector size, n >=32767, it gives segmentation fault, but upto 32766, it runs fine. What could be the error? This is full code.

#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<utility>
#include<algorithm>
#include<sys/time.h>
using namespace std;
#define MAX 100000

bool compare(pair<int,int> p1,pair<int,int> p2) {
    if(p1.second < p2.second)
        return 1;
    else if(p1.second > p2.second)
        return 0;
    if(p1.first <= p2.first)
        return 1;
    else
        return 0;
}

int main() {
    freopen("randomin.txt","r",stdin);
    int n;
    scanf("%d",&n);
    vector< pair<int,int> > p(n);
    for(int i=0;i<n;i++)
        scanf("%d%d",&p[i].first,&p[i].second);
    **printf("%d\n",(int)p.max_size()); // prints 536870911**
    sort(p.begin(),p.begin()+n,compare);

    //for(int i=0;i<n;i++)
        //printf("%d %d\n",p[i].first,p[i].second);
        printf("%.6f\n",(p[n-1].second+p[n-2].second)/(20.0+p[n-1].first+p[n-2].first));

    return 0;
}

回答1:


This may be unrelated to your segmentation fault, but...

In C++, your "compare" predicate must be a strict weak ordering. In particular, "compare(X,X)" must return "false" for any X. In your compare function, if both pairs are identical, you hit the test (p1.first <= p2.first) , and return "true". Therefore, this "compare" predicate does not impose a strict weak ordering, and the result of passing it to "sort" is undefined.




回答2:


Try using all the values from n = 32766 up to 32770. I suspect you'll find that you are experiencing some sort of overflow. This is because 2^15 (32768) is the biggest number that can be represented using 16 bits (assuming you also allow negative numbers). You will have to use a different data type.

Suggestion:

Get it to output the vector's maxsize:

cout << p.max_size();

Let us know what that does. All things being normal, I'd expect it to be in the hundreds of millions (536870911 on my computer). But if it's more like 32768, then that could be the problem.




回答3:


C++ acts weird sometimes. I got Segmentation fault in basic_string.h file! I was upgrading my code base to GCC 5.4.0 from GCC 4.2.1 and suddenly this SEG FAULT occurred and I was wondering why the same code was working earlier and now it is breaking inside C++ own STL. Then I looked up the back trace and found that it was coming from std::sort for vector of pointers, which was using custom comparator to compare the pointers on the basis of fetching the object's name by member function ptr->GetName(). This GetName() was returning a string.

I spent a whole day on figuring out what maybe the change in basic_string file to cause this fault. Then I got to know about this "Strict Weak Ordering" thing and I changed my comparator function accordingly. Still no luck.

Then I tweaked around and changed the original comparator function to a Functor. And now, it worked. I don't know exactly the reason behind it but it worked.

Here is my original comparator function :

bool swap (CLwPatternObj *a, CLwPatternObj *b){
    return a->GetName() < b->GetName() ) 
}

And here is the new Function Object.

class CLwPatternComparator
{
public:
    bool operator() (CLwPatternObj *a, CLwPatternObj *b) 
    {
        return a->GetName() < b->GetName();
    }
};

Same logic, but it works with Function Object.



来源:https://stackoverflow.com/questions/1541817/sort-function-c-segmentation-fault

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!