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.
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.
So my question:
Why this C code is giving segmentation fault error? This code is the implementation of sieve of Eratosthenes.
#include <stdio.h>
#define LIMIT 10000000
#define PRIMES 700000
int is_prime[LIMIT];
int prime[PRIMES];
main()
{
int i, j;
for (i = 0; i < LIMIT; i++)
is_prime[i] = 1;
is_prime[0] = is_prime[1] = 0;
for (i = 0; i < LIMIT; i++)
if (is_prime[i])
for (j = i*i; j < LIMIT; j += i)
is_prime[j] = 0;
j = 0;
for (i = 0; i < LIMIT && j < PRIMES; i++)
if (is_prime[i])
prime[j++] = i;
return 0;
}