c++ - 如何解决非常大的数组输入的 SIGSEGV 错误?

标签 c++ segmentation-fault

所以,我正在解决 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;
}

请帮忙。谢谢。

最佳答案

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

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

关于c++ - 如何解决非常大的数组输入的 SIGSEGV 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54286961/

相关文章:

c++ - 访问类的私有(private)成员函数

c++ - 添加 ~ 10000 个键后,unordered_map 出现段错误

c 段错误 fgets

c++ - 链接到内核

c++ - 二叉搜索树中具有最小值的节点

c++ - 在 C 代码中使用 C++ 库

c - Linux C Shell,子进程抛出段错误

c++ - "Out of bound iterator"为空集

c - 复制到二进制文件时出现段错误

c++ - 如何测试类型转换时间?