my code : https://ide.codingblocks.com/s/214833
Also please explain why we need to declare array as global otherwise it leads to segmentation error in this problem?
my code : https://ide.codingblocks.com/s/214833
Also please explain why we need to declare array as global otherwise it leads to segmentation error in this problem?
Line 29 : for (int i=3; i <= 1000000; i++){
Correction: for (int i=3; i <= 1000000; i+=2)
{
Line 38: for (ll j = i * i; j <= 1000000; j = j+1){
Correction: for (ll j = i * i; j <= 1000000; j = j+i){
Also we need to declare large arrays as global, because if array is local, it’s allocated in the memory block of the function, which is smaller than the whole memory block of program. Segmentation fault means leak in memory, which happens when you run out of allocated memory, which is exactly what happens with very large arrays inside smaller function memory blocks.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.