What is segmentation fault? What are the reasons behind getting segmentation fault? In the following code, it seems to be nothing wrong. But why getting segmentation fault after running the code?
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MX 3200
ll k=0;
ll primes[MX+10];
void sieve()
{
int status[MX];
for(int i=0; i<=MX; i++)
status[i]= 0;
for(int i=3; i*i<=MX; i+=2)
{
if(status[i]==0)
{
for(int j= i*i; j<=MX; j+=i)
{
status[j]= 1;
}
}
}
primes[k]= 2;
k++;
for(int i=3; i<=MX; i+=2)
{
if(status[i]==0)
{
//cout<<i<<endl;
primes[k]= i;
k++;
}
}
}
int main()
{
sieve();
cout<<k<<endl;
for(int i=0; i<k; i++)
cout<< primes[i]<<" ";
return 0;
}