0

Here's the (incomplete) code to find some prime numbers.

#include<iostream>
#include<bitset>
#include<cmath>

#define SQRT_10_POW_12 1000000llu
#define _10_POW_12_BY_2 1000000000000llu/2llu

using namespace std;

int main()
{
    unsigned int T;
    unsigned long long n;
    register unsigned int it1,it2;

    bitset<SQRT_10_POW_12+1llu> isprime;
    bitset<_10_POW_12_BY_2+1llu> nums;

    return 0;
}

The program crashes with SIGSEGV at the declaration of isprime. What is the problem ? How do I Solve this ?

Using Ubuntu 14.04, g++-4.8.1.

kesari
  • 536
  • 1
  • 6
  • 16

1 Answers1

2

While the resulting array may not be really huge, it resides on the stack which has a limited (and platform dependant) size (for example 1MB is a popular size). Try creating the object with the new operator, it will be placed on the heap instead.

Kelm
  • 967
  • 5
  • 14
  • You don't think a a 58GB bitset is huge? – Blastfurnace Dec 06 '14 at 20:20
  • 1
    OP stated he is calculating prime numbers, maybe he has 64+ GB RAM and a powerful super computer at his disposal? Also, that is not the problem he asked about, the crash is caused by a stack overflow. – Kelm Dec 06 '14 at 20:21