由于 C 中的内存不足导致的分段错误 [英] Segmentation fault due to lack of memory in C
本文介绍了由于 C 中的内存不足导致的分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这段代码给了我大约 1/2 的时间段错误:
This code gives me segmentation fault about 1/2 of the time:
int main(int argc, char **argv) {
float test[2619560];
int i;
for(i = 0; i < 2619560; i++)
test[i] = 1.0f;
}
我实际上需要分配一个更大的数组,有没有什么方法可以让操作系统让我获得更多的内存?
I actually need to allocate a much larger array, is there some way of allowing the operating system to allow me get more memory?
我使用的是 Linux Ubuntu 9.10
I am using Linux Ubuntu 9.10
推荐答案
您正在溢出默认的最大堆栈大小,即 8 MB.
You are overflowing the default maximum stack size, which is 8 MB.
您可以增加堆栈大小 - 例如.32 MB:
You can either increase the stack size - eg. for 32 MB:
ulimit -s 32767
...或者您可以使用 malloc
切换到分配:
... or you can switch to allocation with malloc
:
float *test = malloc(2619560 * sizeof test[0]);
这篇关于由于 C 中的内存不足导致的分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文