Infinite loop?

This is a discussion on Infinite loop? within the C++ Programming forums, part of the General Programming Boards category; #include <iostream.h> #include <math.h> void determinePrimes(int); inline bool isPrime(int, int*); int main() { int maxPrime; cin >> maxPrime; determinePrimes(maxPrime); cout ...

  1. #1
    ER
    Guest

    Angry Infinite loop?

    #include <iostream.h>
    #include <math.h>

    void determinePrimes(int);
    inline bool isPrime(int, int*);

    int main()
    {
    int maxPrime;

    cin >> maxPrime;

    determinePrimes(maxPrime);
    cout << "Done.";

    return 0;
    }

    void determinePrimes(int maxPrime)
    {
    int *primes = new int[1.1 * (maxPrime / log10(maxPrime))]; primes[0] = 2;

    for (int j = 3; j <= maxPrime; j += 2)
    if (isPrime(j, primes)) primes[j-3] = j;

    delete[] primes;
    }

    inline bool isPrime(int number, int *primes)
    {
    for (int j = 0; primes[j] < sqrt(number); ++j)
    if (number % primes[j] == 0) return 0;
    return 1;
    }

    ----
    Why does this program crash? It's not making sense to me. I think I've deleted all my pointers and I think I'm not trying to access any arrays out of bounds...

  2. #2
    ER
    Guest

    This is what it should look like in the first post...

    Code:
    #include <iostream.h>
    #include <math.h>
    
    void determinePrimes(int);
    inline bool isPrime(int, int*);
    
    int main()
    {
    	int maxPrime;
    	
    	cin >> maxPrime;
    
    	determinePrimes(maxPrime);
    	cout << "Done.";
    
    	return 0;
    }
    
    void determinePrimes(int maxPrime)
    {
    	int *primes = new int[1.1 * (maxPrime / log10(maxPrime))]; primes[0] = 2; 
    
    	for (int j = 3; j <= maxPrime; j += 2)
    		if (isPrime(j, primes)) primes[j-3] = j;
    
    	delete[] primes;
    }
    
    inline bool isPrime(int number, int *primes)
    {
    	for (int j = 0; primes[j] < sqrt(number); ++j)
    		if (number % primes[j] == 0) return 0;
    	return 1;
    }

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I think I've deleted all my pointers and I think I'm not trying to access any arrays out of bounds...

    The error is in this statement
    primes[j-3] = j;
    Apparently it's an access violation. Your best bet would be to do a paper run of your program and calculate everything by hand to make sure that you're not trying to access a memory location that is outside of the array.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-15-2009, 12:24 AM
  2. Cosine fucntion and infinite loop.
    By youareafever in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 04:45 AM
  3. Infinite Loop with GetAsyncKeyState
    By guitarist809 in forum Windows Programming
    Replies: 1
    Last Post: 04-18-2008, 01:09 PM
  4. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 01:46 AM
  5. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 12:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21