I'm trying to get all prime numbers given a limit. The way I'm doing works fine until 4194304=2^22, but when I try to get all prime numbers until 8388608=2^23 I get the following error message.

#include <iostream> #include <vector> #include <cstring> std::vector<int> eratostenes(int num) { bool primes[num]; std::vector<int> nPrimes; memset(primes, true, sizeof(primes)); for (int p = 2; (p * p) <= num; p++) if (primes[p] == true) for (int i = (p * 2); i < num; i += p) primes[i] = false; for (int p = 2; p < num; p++) if (primes[p]) nPrimes.push_back(p); return nPrimes; } void calcPrimes(int num) { std::vector<int> lsprimes = eratostenes(num); std::cout << "[ " << num << " ] Total of primes: " << lsprimes.size() << std::endl; } int main(int argc, char *argv[]) { int num = std::stoi(argv[1]); calcPrimes(num); return 0; }

Segmentation fault (core dumped)

What is happening and how can I fix this?