" EXC_BAD_ACCESS:无法恢复previously选择框架"错误,数组大小? [英] "EXC_BAD_ACCESS: Unable to restore previously selected frame" Error, Array size?
问题描述
我对创建埃拉托色尼的筛子,并从中拉素数的算法。它可以让你的筛子输入最大值和算法给你的素数低于该值,并存储这些在C风格的数组。
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.
问题:
一切工作正常值高达500.000,但是当我进入一个较大的值助益,而在运行 - 它给了我以x code以下错误信息:
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.)
我的第一个想法是,我并没有用足够大的变数,但我使用'无符号长长整型,这不应该成为问题。此外调试器指向我在我的code的点阵列中的点会被分配一个值。
因此,我想知道是否有一个最大限制到一个数组?如果是的话:我应该使用NSArray中呢?如果没有,那么什么是基于这一信息引起该错误?
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?
编辑:
这是code样子(这是不完全的,因为它未能在公布的最后一行)。我使用的垃圾收集。
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;
}
,因为你可能会或可能不会看到,我是 - 非常much-初学者。其他任何建议,欢迎当然是:)
As you may or may not see, is that I am -very much- a beginner. Any other suggestions are welcome of course :)
推荐答案
您不是四溢的变量;你溢出堆栈。当你创建像在栈上 INT myArray的[500]
,你就宣告500 INT
秒的数组。正常的堆栈大小为8 MB。只有你的两个数组约8.4 MB(8字节* 55万/(1024 ^ 2)= 4.2 MB)。您应该使用的堆内存(从的malloc()
),而不是在这里。因此,这将是这样的:
You're not overflowing the variables; you're overflowing the stack. When you create an array like int myArray[500]
, you're declaring 500 int
s on the stack. The normal stack size is 8 MB. Your two arrays alone are about 8.4 MB (8 bytes * 550000 / (1024^2) = 4.2 MB). You should be using heap memory (from malloc()
) here instead. So it would be like this:
int upperLimit = 550000;
unsigned long long *sieve = malloc(sizeof(long long) * upperLimit);
unsigned long long *primes = malloc(sizeof(long long) * upperLimit);
unsigned long long indexCEX;
unsigned long long primesCounter = 0;
不要忘了,你需要免费()
内存当你用它做,否则你会最终泄漏。
Don't forget that you'll need to free()
the memory when you're done with it or you'll end up leaking.
这篇关于&QUOT; EXC_BAD_ACCESS:无法恢复previously选择框架&QUOT;错误,数组大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!