[英]Got segfault on a macbook pro with a 24+bits indexed array
I am using GCC + the terminal to make an array of prime numbers. 我正在使用GCC +终端制作素数数组。
I once asked on #C@irc.freenode.net, but I still don't understand: 我曾经在#C@irc.freenode.net上问过,但我还是不明白:
An int can store values on 32 bits (so a total of 2^32 unique values), but can't an array have more than 2^24 values ? int可以在32位存储的值(因此总共2 ^ 32个唯一值),但不能阵列具有多于2 ^ 24个值?
I'm not sure, but is Why 24 bits registers? 我不确定,但是为什么要使用24位寄存器? already answers my question ?
已经回答了我的问题?
And does that mean that making an array of long int
doesn't solve the problem ? 这是否意味着将
long int
数组化不能解决问题? Is there some (proportionately fast) way I can bypass this, like using an int[][]
to store those numbers ? 是否有某种(成比例的快速)方法可以绕过此方法,例如使用
int[][]
存储这些数字? Or maybe an include or lib to use an arbitrary number of bytes to store numbers ? 还是include或lib使用任意数量的字节存储数字?
int main()
{
int array1[160000];
printf("first array declared fine.\n");
int array2[170000];
int array3[1600000];
printf("first array declared fine.\n");
int array4[1700000];
int array5[16000000];
printf("first array declared fine.\n");
int array6[17000000];
}
Since you are creating your arrays on the stack, I suppose your segfault is caused by a stack overflow. 由于您是在堆栈上创建数组,因此我想您的段错误是由堆栈溢出引起的。 I would suggest creating such big arrays on the heap, like this:
我建议在堆上创建如此大的数组,如下所示:
int* array = malloc(17000000 * sizeof(int));
So the reason that you get a segfault has nothing to do with using indexes larger than 2^24 but with the fact that the size of all your arrays combined is larger than the size of the stack, causing a stack overflow. 因此,出现段错误的原因与使用大于2 ^ 24的索引无关,而是与所有合并的数组的大小大于堆栈的大小有关,从而导致堆栈溢出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.