maximum size of an array

I am getting the same error on allocating array of size 100001. It shows runtime error on codechef and program stops working in devc++
Here’s snippet of the code:

#include<bits/stdc++.h>
using namespace std;
bool prime[100001];
int k=0;
void sieve(){
	prime[1]=true;
	prime[0]=true;
	for(int i=2;i<=100000;i++){
		if(prime[i]==false){
			for(int j=i*i;j<=100000;j=j+i){
				prime[j]=true;
			}
		}
	}
}

Its beacuse of your

for(int j=i*i;j<=100000;j++) loop

Here your i*i goes out of int bounds and a overflow occurs. Try taking both i and j as long int and it should work fine.

1 Like

Thanks! it helped

depends on your stack size in linux to increase your stack size to unlimited or a particular value use ulimit -s then it will work make sure value is > what ever array space you require plus your auto variables plus return frames etc blah …