title: Weird segmentation fault in a C++ program before main
body: <p>During some memory tests I did I got a segfault from the following program:</p>
  
  <pre><code>#include &lt;string&gt;
  #include &lt;iostream&gt;
  using namespace std;
  
  int main()
  {
      cout &lt;&lt; "Beginning Test" &lt;&lt; endl;
      const int N = 2000000;  
      string sArray[N];
      return 0;
  }
  </code></pre>
  
  <p>Since I got the segfault before "Beginning Test" was printed, I ran it in GDB and checked the backtrace and the only thing I got was:</p>
  
  <blockquote>
    <p>Program received signal SIGSEGV, Segmentation fault.<br>
    0x00000000004008c5 in main () at Main.cxx:11<br>
    11              string sArray[N];<br>
    (gdb) bt<br>
     #0  0x00000000004008c5 in main () at Main.cxx:11</p>
  </blockquote>
  
  <p><strong>The weirdest thing for me is that if I set N to 1000000 (1M) instead of 2000000 (2M) I don't get the segfault</strong>.</p>
  
  <p>Any clue to what the problem might be?</p>
  
  <p>I'm using Linux Red-Hat 2.6.18 and g++ (GCC) 4.1.2.</p>
  
  <p>Thanks!</p>