Stuck with the problem

This is the code I wrote – https://ide.codingblocks.com/s/343931
In this code I tried increasing the size of array p[] to 10^7, but it’s showing segmentation error. I’m unable to devise any other solution for this . Can you please give me a hint as to which direction I should proceed for this question

declare the array globally
als The 500,000th prime is 7,368,787. hence 7400000 is sufficent size of array

i have done some changes in your code
take a look at them
if you have any problem or don’t understand them
feel free to ask

Modified Code

What difference does declaring the array globally makes?

Also in the above code, why is the compiler showing segmentation error, when I make the size of prime array as 500005(since the question asks for primes till 500000 only)?

you can’t declare size of array more than 10^6 inside a function
this is restriction applied by complier to avoid stack overflow
so after declaring the arrays globally this problem resolved

What is the reason for this?

this is because The 500,005th prime is 7,368,841
when you go ahead of this then j is greater than size of array means you are accessing that memory which is not yours
hence segmentation fault occur

But the size of global primes array that you have declared is 5000001 which is obviously greater than 500005, so how come the larger size array is not showing the same error. This is exactly what I’m asking about…

Because I have declare it globally
And in global array max size can be 10^8

I have declared the primes array globally too, but with size 500005 ,instead of 5000001 and the compiler is showing segmentation error. What is the reason for this?

as i explained
i will run till 7400000
but The 500,005th prime is 7,368,841
so j has will exceed 500005 to reach till 740000
hence it will give segmentation fault