bitset declration causes SIGSEGV

124 views Asked by At

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.

1

There are 1 answers

2
Kelm On BEST ANSWER

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.