1

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?

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
M.yue
  • 11
  • 2
  • Your second example does not compile. – PaulMcKenzie Jun 13 '18 at 10:06
  • 1
    memory stack overflow. – Jarod42 Jun 13 '18 at 10:07
  • 1
    Default stack size is smaller than your array size - find the compiler option to increase this to 20M+ or create the array on the heap or use `std::vector`. – Richard Critten Jun 13 '18 at 10:07
  • you are using a variable sized array here, which is not allowed in C++. You need to use `std::vector<>`. and in the second one, how declaring `bool prime[size];` at global will work, as `size` has not been declared and initilized before it. – JeJo Jun 13 '18 at 10:07
  • @JeJo: `size` is constant expression. (misplaced in second example though). – Jarod42 Jun 13 '18 at 10:08
  • @Jarod42 Yes, thats true. How I missed it. – JeJo Jun 13 '18 at 10:08
  • @M.yue: Please let us know if the linked question is a sufficient answer. I was staring to write a detailed answer about exactly how the SIGSEGV is generated, but that's not really a C++ answer. – MSalters Jun 13 '18 at 10:15
  • @MSalters Why not post that answer to the dupe, where people can find it? You could also throw in a remark about `std::vector`, which is currently missing over there. Sounds pretty useful to me. – Baum mit Augen Jun 13 '18 at 10:19
  • @BaummitAugen: Because that question didn't wonder about SIGSEGV. That is to say, there's a reasonable reading of this question which makes it not a duplicate. That's why I'm asking M.yue to clarify what the intent of the question was. – MSalters Jun 13 '18 at 10:24
  • @MSalters Added the missing error message to the dupe target. I'm still quite convinced this is a duplicate. – Baum mit Augen Jun 13 '18 at 10:26
  • @BaummitAugen the debugger of clion did not send out any other error message except "SIGSEGV (Segmentation fault)".That's why I don't understand why the programme crashes. – M.yue Jun 13 '18 at 10:33
  • @MSalters Yes I believe that is a sufficient answer. – M.yue Jun 13 '18 at 10:36
  • @M.yue No worries. It's not an easy error to research as it can have many causes. – Baum mit Augen Jun 13 '18 at 10:47

0 Answers0