7 Replies - 830 Views - Last Post: 13 June 2009 - 07:48 AM Rate Topic: -----

#1 junxuan  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • 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  Icon User is offline

  • D.I.C Regular
  • member icon

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

Re: Stack Overflow?

Posted 13 June 2009 - 06:37 AM

use heap with dynamic
Was This Post Helpful? 1
  • +
  • -

#3 junxuan  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#4 Kanvus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • 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];
Was This Post Helpful? 0
  • +
  • -

#5 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#6 junxuan  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#7 Kanvus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#8 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Stack Overflow?

Posted 13 June 2009 - 07:48 AM

View Postjunxuan, 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1