"Sigabrt Error" - Codechef

Abhinav Arora picture Abhinav Arora · Mar 14, 2014 · Viewed 27k times · Source

The following code ran perfectly in my DEV-C++ compiler but when I submitted in codechef, after running for 3-4 seconds it shows "SIGABRT ERROR". I have researched on this error and have done everything i could to debug, but even after a week I am not able to. Please Help !! Thanks in advance.

For reference question is http://www.codechef.com/problems/LOWSUM

enter code here


void selsort(long long *ssum,long long len)
{
        long long low;
    for(long long i=0;i<len;i++)
    {

    low = ssum[i];
    long long pos=i;
    for(int j=i+1;j<len;j++)
    {

        if(ssum[j]<low)
        {
            low = ssum[j];
            pos = j;

        }

    }


    ssum[pos] = ssum[i];
    ssum[i] = low;
}




}

int main()
{

int t,k,q;
cin>>t;

for(int i=0;i<t;++i)
{
    cin>>k;
    cin>>q;
    long long sq = k*k;

    long long *mot=NULL,*sat=NULL;
    mot = new long long [k];
    sat = new long long [k];
    long long *sum = new long long[sq];
    long long qth;
    long long b=0;

    for(int j=0;j<k;++j)        
    {
        cin>>mot[j];

    }

    for(int j=0;j<k;++j)            
    {
        cin>>sat[j];

    }

    for(int j=0;j<k;++j)            
    {   
        for(int a=0;a<k;++a)
        {
            sum[b] = mot[a]+sat[j];
            ++b;
        }   
    }

    selsort(sum,sq);


    for(int j=0;j<q;++j)
    {
        cout<<"\n";
        cin>>qth;
        cout<<"\n"<<sum[qth-1];
    }

    delete []sum;
    delete []mot;
    delete []sat;
}

return 0;
}

Answer

Namit Sinha picture Namit Sinha · Mar 22, 2014

SIGABRT signal is sent due to many reasons quoting codechef

SIGABRT errors are caused by your program aborting due to a fatal error. In C++, this is normally due to an assert statement in C++ not returning true, but some STL elements can generate this if they try to store too much memory.

In your case it seems to be use of excessive memory

mot = new long long [k];
sat = new long long [k];
long long *sum = new long long[sq];

Note that the value of k can be as large as 20000 so declaring a array of size k will be fine but your sq = k*k which is of order of 4*10^8 which is causing a out of memory problem memory. And your algorithm is also not good enough to give AC within time limit.

Codechef has its own forum to ask such questions, and preferable ways to solve this problem has already been discussed there

http://discuss.codechef.com/problems/LOWSUM