为什么g ++在声明本地函数时声明大数组时显示分段错误,而在全局声明时工作正常? [英] Why does g++ show segmentation fault on declaration of large arrays when declared locally in functions while it works fine when declared globally?

查看:125
本文介绍了为什么g ++在声明本地函数时声明大数组时显示分段错误,而在全局声明时工作正常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想声明4个大小为1000 * 1000(4 * 10 ^ 6 * 4个字节,考虑int)的2-D数组。最初,当我在main()函数中声明这些数组时,我得到了分段错误。

I wanted to declare 4 2-D arrays of size 1000*1000(4*10^6*4 bytes considering int) . Initially, when I declared these arrays in main() function, I was getting segmentation fault.

后来我全局地声明了数组,程序运行得很好。为什么会这样呢?

Later I declared the arrays globally and the program worked fine. Why this is so??

推荐答案

当在函数中声明大型数组时,它们存储在堆栈框架

When large arrays are declared inside a function, they are stored on the stack frame, whose size is not much(don't know the exact value).

当数组被全局声明时,它存储在 Data 如果变量已初始化)和 bss (如果未初始化)。 Data和bss的大小非常高(不知道确切的值),这就是为什么它们可以容纳大型数组声明。

While when the array is declared globally, it is stored in Data (if the variable is initialized) and in bss (if not initialized). The size of Data and bss is very high(don't know the exact value), that is why they could accommodate large array declarations.

这篇关于为什么g ++在声明本地函数时声明大数组时显示分段错误,而在全局声明时工作正常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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