为什么我在宣布堆栈上的大阵获得在C段错误? [英] Why do I get a segfault in C from declaring a large array on the stack?

查看:93
本文介绍了为什么我在宣布堆栈上的大阵获得在C段错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到这一行$​​ C $ C的段错误:

I get a segfault from this line of code:

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

这是为什么?

推荐答案

您正试图分配 1820 * 285 * 5 *的sizeof(INT)字节= 10MB左右(如果的sizeof(int)的== 4 )。这可能更多的字节比你的OS为您提供了默认的堆栈分配,所以你得到一个堆栈溢出/段错误。

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.

这篇关于为什么我在宣布堆栈上的大阵获得在C段错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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