Why do I get a segfault in C from declaring a large array on the stack?

前端 未结 5 584
野趣味
野趣味 2020-11-27 08:59

I get a segfault from this line of code:

int fatblob[1820][286][5];

Why is that?

5条回答
  •  佛祖请我去吃肉
    2020-11-27 09:01

    int fatblob[1820][286][5];
    

    you are trying to allocate a memory location of 180*286*5*4 (let size of int = 4) that is around 9.8 MB, so there is a possibility of having lesser default stack size on your OS.

    LINUX has 8192 KB(i.e. 8 MB) of stack size. So obviously you will be getting a stack overflow if you are trying to allocate more memory on stack.

    You can try changing the stack size of your OS. In LINUX you can try ulimit

    ulimit -s < whateversize you want it to be>

    like

    $ ulimit -s 1024

    I hope it will help you.

提交回复
热议问题