【问题标题】:Trying to print a variable. Program crashes [duplicate]试图打印一个变量。程序崩溃[重复]
【发布时间】:2014-10-30 13:18:02
【问题描述】:
#include <iostream>
#include <math.h>

using namespace std;

int main()
{
    int64_t sum=0, c=-1, li[1000000];
    bool flag = 1;
    for(int i=2; i<=2000000; i++)
    {
        flag = 1;
        for(int j = 0; j<c; j++)
        {
            if (i%li[j]==0)
            {
                flag = 0;
                break;
            }
        }
        if(flag)
        {
            sum += i;
            li[++c] = i;
        }
    }
    cout<<"done"<<endl;
    cout<<sum;             // This line causes the program to crash.
    return 0;

}

查看上面的代码。 “库特

使用 Code::Blocks v13.12,GNU GCC 编译器,默认设置。 另外,我是编码和 StackOverflow 的新手 :)

【问题讨论】:

  • 堆栈溢出老兄...
  • 使用std::vector&lt;int64_t&gt;

标签: c++ crash cout


【解决方案1】:

对于堆栈来说,这个巨大的数组可能太大了 - 通常,堆栈被限制为几兆字节,有时在资源有限的平台上更少。

改用动态数组:

std::vector<int64_t> li(1000000);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多