The size of the array in C++?

The code uses the Sieve of Eratosthenes (with the argument n <= 1.000.000 everything works fine:
#include <iostream>
#include <cstdlib>
#include <cmath>

using namespace std;

void fff(int n) {

 int s[n+1], j = 0;

 s[1] = 0;

 for (int i = 2; i <= n; i++) {
 s[i] = 1;
}

 for (int i = 2; i * i <= n; i++) {
 if (s[i] == 1) {
 for (int l = i * i; l <= n; l += i) {
 s[l] = 0;
}
}
}

 for (int i = 2; i <= n; i++) {
 if (s[i] == 1) {
 cout << i << " ";
j++;
}
}

 cout << endl;
 cout << j;
}

int main() {
fff(1000000);

 return 0;
}

The output of the program:
2 3 5 7 ...
78498
Process finished with exit code 0

But when n >= 10.000.000 the program does not output anything except:
Process finished with exit code 11
What's the problem?
March 12th 20 at 08:09
3 answers
March 12th 20 at 08:11
Solution
Code 11 segmentation fault.
The array s is put on the stack, the stack size is limited.
how to increase the stack size? - summer_Schuster commented on March 12th 20 at 08:14
@summer_Schuster, it is not necessary to increase the stack size, create the array in the heap. - beulah_Rowe7 commented on March 12th 20 at 08:17
March 12th 20 at 08:13
It's C++, and therefore you must have a very good reason not to use std::vector in this case.
std::array then it is - ozella_Swaniaws commented on March 12th 20 at 08:16
@ozella_Swaniaws, No you can not, because in this case it just will not compile. - Sterling_Volkm commented on March 12th 20 at 08:19
@Sterling_Volkm, Well, the idea and the code from the example question, too, will not compile. - ozella_Swaniaws commented on March 12th 20 at 08:22
@ozella_Swaniaws, probably :) - Sterling_Volkm commented on March 12th 20 at 08:25
@ozella_Swaniaws,
compiles despite VLA.

std::vector in this case.

std::vector<bool>

only it is all to no purpose. - claire_Hessel16 commented on March 12th 20 at 08:28
March 12th 20 at 08:15
What to do nobody wrote. Though obviously I'll put my 3 pennies.
You can use new[], std::array or std::vector, or to reconfigure the system for a larger stack

Find more questions by tags C++