0

I was trying to write the following program for a programming contest (already over). However, I keep getting the SIGABRT error. I make sure to empty all the containers before I run the next test case. What should I check?

This is how I am implementing the code: http://discuss.codechef.com/questions/29659/lowsum-editorial

#include<stdio.h>
#include<algorithm>
#include<queue>
#include<vector>

using namespace std;

class compare
{
public:
    bool operator()(int a, int b)
    {
        return a>b;
    }
};

int main()
{
    int T,K,Q;
    vector<int> A,B,q,nsum;
    priority_queue<int, vector<int> , compare> PQ;      //min PQ
    scanf("%d",&T);

while(T--)
{
    scanf("%d %d",&K,&Q);

    A.resize(K);
    B.resize(K);
    q.resize(Q);

    for(int i= 0;i<K;++i)
        scanf("%d",&A[i]);

    for(int i=0;i<K;++i)
        scanf("%d",&B[i]);

    int max_q=0;

    for(int i=0;i<Q;++i)
    {
       scanf("%d",&q[i]);
       if(q[i]>max_q)
        max_q=q[i];
    }

    sort(A.begin(),A.end());
    sort(B.begin(),B.end());

    while(!PQ.empty())
        PQ.pop();

    int j=0;

    while(max_q > 0 && j < K)
    {
        for(int i=0;i<K;++i)
            PQ.push(A[i]+B[j]);

        max_q--;
        j++;                        //next element of B[]
    }

    while(!PQ.empty())
    {
        nsum.push_back(PQ.top());
        PQ.pop();
    }

    for(int j=0;j<Q;++j)
       printf("%d\n",nsum[q[j]-1]);

    nsum.clear();
}
}
user2441151
  • 1,522
  • 3
  • 17
  • 26
  • 4
    Have you tried running in a debugger? – Some programmer dude Nov 28 '13 at 12:32
  • The given test case runs fine on my PC. It is when I submit to the judge that I get the error. It must be a certain test case that is causing it and I can't find one on my own. :( What are the general reasons? – user2441151 Nov 28 '13 at 12:34
  • SIGABRT can come from inside or outside the program, but usually inside. Run it in a debugger. If the `abort()` function is in the call stack, it means it is coming from within the program, probably due to a programming error resulting in the runtime calling `abort()`. – Ben Nov 28 '13 at 13:01
  • Post a stack trace of the problem (or run through valgrind). Don't waste time asking us to speculate or debug your code for you, go straight to the line number with the problem. – Nicholas Wilson Nov 28 '13 at 13:20

1 Answers1

0

I think you're going over memory.

Resize the arrays to 0 first. Otherwise, resize is supposed to copy the content that is common in the two arrays, so it will need to first allocate a new entrie array, copy everything, then release the old one.

Also for these sort of things you can get away resizing the vectors at the begging to cope with the largest test case possible, and then never touching them again.

Sorin
  • 11,863
  • 22
  • 26