分割故障 - 大阵

问题描述:

我有一个奇怪的错误,当我尝试编译我的code,它是用C写的错误说:

I am having an odd error when I try to compile my code, which is written in C. The error says

 segmentation fault (core dumped)

在我的code,我有很多真正的大双阵列(如接近10的长度尺寸)。我初始化双打的一个阵列,当我尝试随即相同的大小(大约100,000长度)来初始化数组它给我的分段错误。奇怪的是,这取决于阵列的大小。例如,如果我这样做

In my code, I have a lot of really large double arrays (like of sizes close to 100,000 in length). I initialize one array of doubles and when I try to initialize an array immediately afterwards of the same size (roughly a 100,000 length) it gives me the segmentation fault error. Oddly, it depends on the size of the array. For example if I do

 double arr[70000];       

它给了我分段错误,但

It gives me the segmentation error but

 double arr[60000];     

不给我的错误。我正在我的code在Linux机器上是否有帮助。我真的需要很多不同的超大型双阵列。这是怎么回事?

does not gives me the error. I am running my code on a linux machine if that helps. I really need many different very large double arrays. What is going on?

您已经遇到了堆栈溢出;基本上,你已经用尽可用的堆栈空间给程序。

You've encountered a "Stack Overflow"; basically, you've exhausted the stack space available to your program.

如果您在堆上分配的数组(以堆存储),你可能会好起来的。

If you allocate the arrays on the heap (in heap storage), you probably will be okay.

使用C,你很可能使用 的malloc 指令分配内存。

With C, you'd likely use the malloc instruction to allocate the memory.

当然,你会记得使用 免费 当你用它做指令返回的内存。

And of course, you'll remember to use the free instruction to return the memory when you're done with it.