分段故障,由于在C内存不足 [英] Segmentation fault due to lack of memory in C

查看:186
本文介绍了分段故障,由于在C内存不足的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这code给我分割故障有关的时间的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

float *test = malloc(2619560 * sizeof test[0]);

这篇关于分段故障,由于在C内存不足的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆