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

}

Check out the code above. The "cout<

Was using Code::Blocks v13.12, GNU GCC compiler, default settings. Also, I'm a newbie, both to coding and StackOverflow :)

Agnivesh
  • 3
  • 2

1 Answers1

2

The gigantic array is probably too large for the stack - typically, the stack is limited to a small number of megabytes, and sometimes much less on platforms with limited resources.

Use a dynamic array instead:

std::vector<int64_t> li(1000000);
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644