首页 > 解决方案 > 如何解决非常大的数组输入的 SIGSEGV 错误?

问题描述

因此,我正在解决 HackerEarth 中的一个问题,该问题将您的代码测试到非常大的测试用例中。因此,当我尝试提交代码时,它通过了前 6 个测试用例,对于其他 5 个测试用例,它给出了“超出时间限制”,对于所有其他测试用例,它给出了 SIGSEGV 信号。

这是代码:

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

请帮忙。谢谢你。

标签: c++segmentation-fault

解决方案


您正在尝试访问您无法访问的内存。这就是 SIGSEGV 的原因。您需要静态而不是动态地声明数组大小。

但是,最好使用向量,因为您使用的是 C++。


推荐阅读