I tried to initiate a bool array and its size is 20000001.The code looks like this:
#include <iostream>
#include <cstring>
using namespace std;
int main(){
const unsigned long long int size = 20000001;
bool prime[size];
return 0;
}
Then I compile it successfully but when I run it, Windows gives a message of stop running and GDB only throws SIGSEGV (Segmentation fault)
.
But after I initiate the array (same size) outside main function, the code looks like this:
#include <iostream>
#include <cstring>
using namespace std;
bool prime[size];
int main(){
const unsigned long long int size = 20000001;
memset(prime,true,size);
return 0;
}
No error is thrown and the program works smoothly.
So my questions are, what causes the SIGSEGV signal? Is it related to overflow of stack?