“Sigabrt Error” - Codechef

血红的双手。 提交于 2019-12-11 02:20:06

问题


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;
}

回答1:


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



来源:https://stackoverflow.com/questions/22415362/sigabrt-error-codechef

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