Stack Overflow?

Page 1 of 1

7 Replies - 1149 Views - Last Post: 13 June 2009 - 07:48 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=109788&amp;s=fa1e7eff2402f5c7023b39761b407f3e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 junxuan

Reputation: 4
• Posts: 44
• Joined: 25-March 09

Stack Overflow?

Posted 13 June 2009 - 06:29 AM

im making a prime number generator base on the sieve of erablabla...

it works properly however it is unable to generate prime numbers up to 1 million or so as i would get a stack overflow
i hope some pro can help point out why it causes a stack overflow and how to prevent it in the future.

Heres my code btw:
```int genPrimes(long primes[], long primeSize){
long sum = 0;
for (long i = 2; i < primeSize; i++){
if (primes[i] == 0 || primes[i] == 1){
continue;
}
int currentPrime = primes[i];
cout << currentPrime << " ";
sum += currentPrime;
for (long k = 2; k < primeSize; k++){
if (primes[k] % currentPrime == 0){
primes[k] = 1;
}
}
}
return 1;
}
```

Is This A Good Question/Topic? 0

Replies To: Stack Overflow?

#2 Kanvus

• D.I.C Regular

Reputation: 42
• Posts: 452
• Joined: 19-February 09

Re: Stack Overflow?

Posted 13 June 2009 - 06:37 AM

use heap with dynamic

#3 junxuan

Reputation: 4
• Posts: 44
• Joined: 25-March 09

Re: Stack Overflow?

Posted 13 June 2009 - 06:48 AM

could you elaborate, im not sure what you are talking about o.o

#4 Kanvus

• D.I.C Regular

Reputation: 42
• Posts: 452
• Joined: 19-February 09

Re: Stack Overflow?

Posted 13 June 2009 - 07:03 AM

Okay, stack is one part of memory. It runs out. Use the memory body in your system called Heap by allocating large pointer space.

long* plap = new long[500000];

#5 janotte

• code > sword

Reputation: 991
• Posts: 5,141
• Joined: 28-September 06

Re: Stack Overflow?

Posted 13 June 2009 - 07:04 AM

Consider "sum".
As each "currentPrime" gets added to it it starts to grow very rapidly.

Then have a look at the size of the various datatypes in the table here:
http://www.cplusplus...rial/variables/

Do you see what is happening?

#6 junxuan

Reputation: 4
• Posts: 44
• Joined: 25-March 09

Re: Stack Overflow?

Posted 13 June 2009 - 07:04 AM

oo ok ill try that means that stack is limited not by the RAM?

@jannote : seems like the long datatype isnt large enuff to hold the value?

This post has been edited by junxuan: 13 June 2009 - 07:06 AM

#7 Kanvus

• D.I.C Regular

Reputation: 42
• Posts: 452
• Joined: 19-February 09

Re: Stack Overflow?

Posted 13 June 2009 - 07:31 AM

then BigInt vectors are needed to hold infinitely large amount of digits. explaining bigint is a pain

#8 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,232
• Joined: 14-September 07

Re: Stack Overflow?

Posted 13 June 2009 - 07:48 AM

junxuan, on 13 Jun, 2009 - 07:04 AM, said:

oo ok ill try that means that stack is limited not by the RAM?

We could fill up a few pages on various ways the OS allocates stack memory to programs, but it will be sufficient enough here to simply say, upon execution, the OS allocates a segment of memory for your program that is referred to as the program stack. You don't get all of the available RAM.