Kushagra Manish
Kushagra Manish

Reputation: 1

How to solve SIGSEGV error for very large array inputs?

So, I was solving a problem in HackerEarth which tests your code to very large testcases. Thus, when I tried to submit the code , it passed for first 6 testcases, for 5 others , it gave "Time Limit Exceeded" and for all others, SIGSEGV Signal .

Here's the code:

#include<bits/stdc++.h>
#include<cmath>
using namespace std;

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long unsigned int tc,b, a , d,c;
cin>>tc;
for(int i=0;i<tc;++i)
{
    cin>>a;
    cin>>d;
    cin>>c;
    cin>>b;
    long long unsigned int arr[b+1];
    arr[0]=a;
    arr[1]=d;
    arr[2]=c;



    if(b>=3){

        for(long long unsigned i=3;i<=b;++i)
        {
            // arr[i]%=1000000007;
            (arr[i])=(arr[i-1]+3*arr[i-3]+2*i)%1000000007;
            arr[i]%=1000000007;
        }
        printf("%llu \n",arr[b]%1000000007);



    }
    else{
        printf("%llu \n",arr[b]%1000000007);
    }
}
return 0;
}

Please help. Thank you.

Upvotes: 0

Views: 97

Answers (1)

Pratik Singhal
Pratik Singhal

Reputation: 6492

You are trying to access memory that is not accessible to you. That's the reason for SIGSEGV. You need to declare the array size statically and not dynamically.

However, it is better to use vector, since you are using C++.

Upvotes: 1

Related Questions