title: Cygwin g++ x86_64 segmentation fault (core dumped) when using > 2GB memory
body: <p>I've written a prime sieve program in c++, which uses ~12GB ram to calculate all primes below 100,000,000,000 (100 Billion). </p>
  
  <p>The program works fine when compiled with Visual Studio 2012 (in a project set up for x64) as well as g++ on 64 bit linux. However, when compiled with g++ in cygwin64 on Windows 7 Home Premium 64 bit, a segmentation fault occurs when attempting to use more than ~2GB ram (running the sieve for > ~17,000,000,000) </p>
  
  <p>I'm fairly sure it's running as a 64 bit process as there's no *32 next to the process name in task manager.</p>
  
  <p>The code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;cmath&gt;
  #include &lt;cstdlib&gt;
  using namespace std;
  
  long long sieve(long long n);
  
  int main(int argc, char** argv) {
      const long long ONE_BILLION = 1000*1000*1000;
      if(argc == 2)
          cout &lt;&lt; sieve(atol(argv[1])) &lt;&lt; endl;
      else
          cout &lt;&lt; sieve(ONE_BILLION * 100) &lt;&lt; endl;
  }
  
  long long sieve(long long n) {
      vector&lt;bool&gt; bools(n+1);
      for(long long i = 0; i &lt;=n; i++) 
          bools[i] = true;
  
      double csqrtn = sqrt(n);
      for (long long i = 2; i &lt; csqrtn; ++i)
          if (bools[i]) 
              for (long long j = i * i; j &lt; n; j += i) 
                  bools[j] = false;
  
      long long primes2 = 0;
      for (long long i = 2; i &lt; n; i++) 
          if (bools[i]) 
              primes2++;
  
      return primes2;
  }
  </code></pre>
  
  <p>Working fine in Visual studio:</p>
  
  <p><img src="https://i.stack.imgur.com/Mpq0n.png" alt="enter image description here"></p>
  
  <p>Working fine on x64 linux:</p>
  
  <p><img src="https://i.stack.imgur.com/m7KkN.png" alt="enter image description here"></p>
  
  <p>Compiled with the command:</p>
  
  <p><code>$ g++ -O3 sieve.cpp -o sieve.exe</code></p>
  
  <p>Running for 18 billion fails: </p>
  
  <pre><code>$ ./sieve.exe 18000000000
  Segmentation fault (core dumped)
  </code></pre>
  
  <p>Works fine (using 2,079,968 K memory according to task manager, though my reputation doesn't allow me to post a third link.)</p>
  
  <pre><code>$ ./sieve.exe 17000000000
  755305935
  </code></pre>
  
  <p>g++ version:</p>
  
  <pre><code>$ g++ --version
  g++ (GCC) 4.8.1
  Copyright (C) 2013 Free Software Foundation, Inc.
  This is free software; see the source for copying conditions.  There is NO
  warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  </code></pre>
  
  <p>Note: if you are going to try and run this yourself, it can take quite a long time. On a 3570k @ 4.2GHz running 100 billion in visual studio takes around 30 mins, 1 billion around 10 seconds. However you might be able to duplicate the error with just the vector allocation.</p>
  
  <p>Edit: since I didn't explicitly put a question: Why does this happen? Is it a limitation of the cygwin64 dll (cygwin64 was only released fully about a month ago)?</p>