Hi there,
I have an algorithm for creating the sieve of Eratosthenes and pulling primes from it. It lets you enter a max value for the sieve and the algorithm gives you the primes below that value and stores these in a c-style array.
Problem: Everything works fine with values up to 500.000, however when I enter a large value -while running- it gives me the following error message in xcode:
Program received signal: “EXC_BAD_ACCESS”.
warning: Unable to restore previously selected frame.
Data Formatters temporarily unavailable, will re-try after a 'continue'. (Not safe to call dlopen at this time.)
My first idea was that I didn't use large enough variables, but as I am using 'unsigned long long int', this should not be the problem. Also the debugger points me to a point in my code where a point in the array get assigned a value. Therefore I wonder is there a maximum limit to an array? If yes: should I use NSArray instead? If no, then what is causing this error based on this information?
EDIT: This is what the code looks like (it's not complete, for it fails at the last line posted). I'm using garbage collection.
/*--------------------------SET UP--------------------------*/
unsigned long long int upperLimit = 550000; //
unsigned long long int sieve[upperLimit];
unsigned long long int primes[upperLimit];
unsigned long long int indexCEX;
unsigned long long int primesCounter = 0;
// Fill sieve with 2 to upperLimit
for(unsigned long long int indexA = 0; indexA < upperLimit-1; ++indexA) {
sieve[indexA] = indexA+2;
}
unsigned long long int prime = 2;
/*-------------------------CHECK & FIND----------------------------*/
while(!((prime*prime) > upperLimit)) {
//check off all multiples of prime
for(unsigned long long int indexB = prime-2; indexB < upperLimit-1; ++indexB) {
// Multiple of prime = 0
if(sieve[indexB] != 0) {
if(sieve[indexB] % prime == 0) {
sieve[indexB] = 0;
}
}
}
/*---------------- Search for next prime ---------------*/
// index of current prime + 1
unsigned long long int indexC = prime - 1;
while(sieve[indexC] == 0) {
++indexC;
}
prime = sieve[indexC];
// Store prime in primes[]
primes[primesCounter] = prime; // This is where the code fails if upperLimit > 500000
++primesCounter;
indexCEX = indexC + 1;
}
As you may or may not see, is that I am -very much- a beginner. Any other suggestions are welcome of course :)