Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何修复我的程序崩溃?_C++ - Fatal编程技术网

C++ 如何修复我的程序崩溃?

C++ 如何修复我的程序崩溃?,c++,C++,我正在为projecteuler.net的第7个问题编写代码,我有点成功了,但我需要找到第10001个素数,如果我试图找到大于2262的素数,我的程序就会崩溃,因此我无法得到我需要的答案。我已经尝试过将int改为其他数据类型,比如long,但这似乎不是问题,我只是被卡住了。当我试图超过2262素数时,我需要做什么才能使我的程序不崩溃 int main() { int numbersList[10000], prime[10000], rez; int where1=1, where=0; boo

我正在为projecteuler.net的第7个问题编写代码,我有点成功了,但我需要找到第10001个素数,如果我试图找到大于2262的素数,我的程序就会崩溃,因此我无法得到我需要的答案。我已经尝试过将int改为其他数据类型,比如long,但这似乎不是问题,我只是被卡住了。当我试图超过2262素数时,我需要做什么才能使我的程序不崩溃

int main()
{
int numbersList[10000], prime[10000], rez;
int where1=1, where=0;
bool remainder;
prime[0] = 2;

for(int i = 2; i < INT_MAX; i++)
{
    numbersList[where] = i;//2
    where++;
    for(int j = 0; j < where1; j++)
    {
        if(i % numbersList[j] != 0)
        {
            remainder = true;
        }
        else
        {
            remainder = false;
            break;
        }
    }
    if(remainder)
    {
        prime[where1] = i;
        where1++;
    }
    if(where1==2262)// Which primary number you want. More or equal to 2263 crashes.
    {
        rez = prime[where1-1];
        break;
    }
}
cout << endl << where1 << " primary number: " << rez << endl;
return 0;
}
intmain()
{
整数列表[10000],素数[10000],rez;
int,其中1=1,其中=0;
布尔余数;
素数[0]=2;
对于(int i=2;i我想它有一个小的打字错误。
模数运算将使用
prime
数组而不是
numberList
数组来完成

int main()
{
int numbersList[1000000], prime[100000], rez;
int where1=1, where=0;
bool remainder;
prime[0] = 2;

for(int i = 2; i < 1000000; i++)
{
    numbersList[where] = i;//2
    where++;
    remainder = true;
    for(int j = 0; j < where1; j++)
    {
        if(i % prime[j] != 0)
        {
            remainder = true;
        }
        else
        {
            remainder = false;
            break;
        }
    }
    if(remainder)
    {
        prime[where1] = i;
        where1++;
    }
    if(where1==10001)//104743
    {
        rez = prime[where1-1];
        break;
    }
}
cout << endl << where1 << " primary number: " << rez << endl;
return 0;
}
intmain()
{
整数列表[1000000],素数[100000],rez;
int,其中1=1,其中=0;
布尔余数;
素数[0]=2;
对于(int i=2;i<1000000;i++)
{
numbersList[where]=i;//2
where++;
余数=真;
对于(int j=0;j<1;j++)
{
如果(i%prime[j]!=0)
{
余数=真;
}
其他的
{
余数=假;
打破
}
}
如果(余数)
{
素数[其中1]=i;
其中1++;
}
如果(其中1==10001)//104743
{
rez=素数[1-1];
打破
}
}

cout看起来好像超出了数组的范围。您没有检查
j
where
where1
是否小于10000。在大多数平台上,INT\u MAX将大于10000。我刚刚在Visual Studio中调试了此项。该程序确实在
numbersList[其中]崩溃=i;//2
我的第一条评论是正确的。
where
超过10000导致了未定义的行为。这一行仍然是一个问题
numbersList[where]=i;//2
虽然现在不需要。是的,它将能够存储
1000000
个数字。不使用此
int numbersList[10000]
如果您增加了限制,可能会出现堆栈溢出。尽管更新的代码中没有使用
numbersList