title: Mysterious Memory Limit Exceeded; Segmentation Fault error on sieve of Eratosthenes algorithm
body: <p>So, segmentation fault, or memory limit exceeded error occurs when, program tries to access memory out of program stack or you have no more memory left to be allocated to the program.</p>
  
  <p>Edit: When I wrote this question, I was unaware about debugger in programming language. But now that I know, this program runs and all the memory of arrays are allocated in the start of the program.</p>
  
  <p>So my question:</p>
  
  <p>Why this C code is giving segmentation fault error?
  This code is the implementation of sieve of Eratosthenes.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  #define LIMIT   10000000
  #define PRIMES  700000
  int is_prime[LIMIT];
  int prime[PRIMES];
  
  main()
  {
      int i, j;
      for (i = 0; i &lt; LIMIT; i++)
          is_prime[i] = 1;
      is_prime[0] = is_prime[1] = 0;
      for (i = 0; i &lt; LIMIT; i++)
          if (is_prime[i])
              for (j = i*i; j &lt; LIMIT; j += i)
                  is_prime[j] = 0;
  
      j = 0;
      for (i = 0; i &lt; LIMIT &amp;&amp; j &lt; PRIMES; i++)
          if (is_prime[i])
              prime[j++] = i;
  
      return 0;
  }
  </code></pre>