Q:

How can I reserve memory for a very large sieve?

I want to generate primes by sieving up to 100,000,000 but declaring a bool array of this range is crashing my program.
This is my code:
long long i,j,n;
bool prime[100000000+1];
prime[1]=prime[0]=false;
for(i=2;i<=100000000;i++){
    prime[i]=true;
}
for(i=2;i<=100000000;i++){
    if(prime[i]==false){
        continue;
    }
    for(j=i*2;j<=100000000;j+=i){
        prime[j]=false;
    }
}

How can I solve this problem?

A:

The size of the array prime is 100 MB and declaring so big array on the stack is not allowed. Try placing the array in global scope to allocated it on the heap or alternatively allocate it using new(in C++) or malloc(in C). Don't forget to free the memory after that!

A:

Variables can be stored in three different memory areas: static memory, automatic memory, dynamic memory. Automatic memory (non-static local variables) has limited size, you crossed it, and this crashed the program. The alternative is to mark your array static, which will place your array in static storage, or use dynamic memory.
Since this is tagged C++...
Use std::vector which is simple to use and uses dynamic memory.
#include <vector>
//...
//...
long long i,j,n;
std::vector<bool> prime(100000000+1, true);
prime[1]=prime[0]=false;
for(i=2;i<=100000000;i++){
    if(prime[i]==false){
        continue;
    }
    for(j=i*2;j<=100000000;j+=i){
        prime[j]=false;
    }
}

std::vector<bool> uses "bit-efficient" representation, which means that std::vector here will take about eight1 times less memory than traditional array.
std::bitset is similiar, but is constant in size, and you have to mark it static to avoid taking space in automatic memory.
You haven't asked, but Erastostenes Sieve is not the fastest algorithm for calculating a lot of prime numbers. It seems that Sieve of Atkin is faster and uses less memory.
1 - When your system has 8-bit bytes.

