可以在 GCC 中声明的静态数组的最大大小是多少? [英] What is the maximum size of static array that can be declared in GCC?

查看:47
本文介绍了可以在 GCC 中声明的静态数组的最大大小是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它是如何确定的?这是否取决于编译器/架构/主机系统?

How its determined ? Does this depend on the compiler/Architecture/Host system ?

例子:

int array[0x8000000000000000]; 

对于 x86_64 位系统 GCC 输出中的这一行:

For this line in a x86_64 bit system GCC outputs:

Error "size of array 'array' is too large".

推荐答案

我假设静态数组是指固定长度的数组(静态分配,如 int array[SIZE],而不是动态分配分配).数组大小限制应该取决于声明的数组的范围.

By static array, I assume, you mean a fixed length array (statically allocated, like int array[SIZE], not dynamically allocated). Array size limit should depend on the scope of the array declared.

  • 如果您在本地范围内(在某个例程内)声明了数组,则大小限制由堆栈大小决定.
  • 如果 gcc 在 linux 上运行,堆栈大小由一些环境变量决定.使用 ulimit -a 查看并使用 ulimit -s STACK_SIZE 修改堆栈大小.
  • 如果 gcc 在 windows(如 MinGW)上运行,堆栈大小可以通过 gcc -Wl,--stack, STACK_SIZE 指定.
  • 如果您已在全局范围内声明了数组,则该数组将存储在 DATA 或 BSS 部分(分别取决于数组是初始化还是未初始化).DATA 和 BSS 部分的大小由底层操作系统决定.
  • 如果您已在静态范围内声明了数组(如 static int array[SIZE]),那么再次,该数组将存储在 DATA 或 BSS 部分中(取决于数组是否已初始化或未初始化分别).DATA 和 BSS 部分的大小由底层操作系统决定.
  • If you have declared the array in local scope (inside some routine), size limit is determined by stack size.
  • If gcc is running on linux, the stack size is determined by some environment variable. Use ulimit -a to view and ulimit -s STACK_SIZE to modify the stack size.
  • If gcc is running on windows (like MinGW), stack size can be specified by gcc -Wl,--stack, STACK_SIZE.
  • If you have declared the array in global scope, the array is stored in DATA or BSS section (based on whether the array is initialized or uninitialized respectively). The DATA and BSS section size are determined by underlying OS.
  • If you have declared the array in static scope (like static int array[SIZE]), again, the array is stored in DATA or BSS section (based on whether the array is initialized or uninitialized respectively). The DATA and BSS section size are determined by underlying OS.

这篇关于可以在 GCC 中声明的静态数组的最大大小是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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