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

前端 未结 5 575
野趣味
野趣味 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:15

    You're trying to allocate 1820 * 285 * 5 * sizeof(int) bytes = about 10MB (if sizeof(int) == 4). That's probably more bytes than your OS gives you for stack allocation by default, so you get a stack overflow/segfault.

    You can fix this by either asking for extra stack when you create the thread, allocating on the heap, or changing the OS defaults.

提交回复
热议问题