Too Large of an Array

Hey! I've been trying to get the answer to Project Euler problem 10 and am having some troubles :o
I'm using the standard Sieve of Eratosthenes approach to the problem. My problem is that every time i try to make my sieve have 2000000 slots the program crashes! Everything works perfectly if I'm just getting for example the sum of the primes below 10. Please help me, thanks!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include<iostream>
using std::cout;
using std::endl;

const int max = 10;

class CPrime
{
public:
	CPrime()
	{
		sieve[0] = 0;
		sieve[1] = 0;
		for(int i = 1; i < max; i++)
		{
			sieve[i] = i;
		}
	}
	
	void generateSieve();
	void showSieve();
	int addSieve();
private:
	int sieve[max];
};

void CPrime::generateSieve()
{
	for(int i = 2; i*i < max; i++)
	{
		for(int y = i+i; y < max; y += i)
		{
			sieve[y] = 0;	
		}
	}

	return;
}

void CPrime::showSieve()
{
	for(int i = 0; i < max; i++)
		if(sieve[i] != 0)
			cout << sieve[i] << endl;
	
	return;
}

int CPrime::addSieve()
{
	int total = 0;
	for(int i = 2; i < max; i++)
		total += sieve[i];

	return total;
}

int main(void)
{	
    CPrime primes;
	primes.generateSieve();
	primes.showSieve();
	cout << primes.addSieve();

	cout << endl;
	return 0;
}


The problem can be found here: http://projecteuler.net/index.php?section=problems&id=10
You should use a vector instead of a raw array (or use new to create your class instance). In particular, you should use a vector<bool>, which cuts down memory usage to 1/32 of what it is now.
You're making your object on the stack. The stack has a limited size. If your object is too big, your stack overflows.

http://en.wikipedia.org/wiki/Stack_overflow#Very_large_stack_variables
thank you both! thank you for the article moschops, when i allocated it dynamically it worked with the following code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include<iostream>
using std::cout;
using std::endl;

const int max = 2000000;

class CPrime
{
public:
	CPrime()
	{
		sieve = new int[max];
		sieve[0] = 0;
		sieve[1] = 0;
		for(int i = 1; i < max; i++)
		{
			sieve[i] = i;
		}
	}

	~CPrime()
	{
		delete[] sieve;
	}
	
	void generateSieve();
	void showSieve();
	int addSieve();
private:
	int* sieve;
};

void CPrime::generateSieve()
{
	for(int i = 2; i*i < max; i++)
	{
		for(int y = i+i; y < max; y += i)
		{
			sieve[y] = 0;	
		}
	}

	return;
}

void CPrime::showSieve()
{
	for(int i = 0; i < max; i++)
		if(sieve[i] != 0)
			cout << sieve[i] << endl;
	
	return;
}

int CPrime::addSieve()
{
	int total = 0;
	for(int i = 2; i < max; i++)
		total += sieve[i];

	return total;
}

int main(void)
{	
    CPrime primes;
	primes.generateSieve();
	cout << primes.addSieve();

	cout << endl;
	return 0;
}


would you mind doing me one last favor XD, project euler doesnt work for me so could you tell me if my answer of 1179908154 is right?? thank you!
Topic archived. No new replies allowed.