可以在GCC中声明的静态数组的最大大小是多少?

时间:2022-08-26 21:48:12

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

怎么决定?这取决于编译器/架构/主机系统吗?

Example:

int array[0x8000000000000000]; 

For this line in a x86_64 bit system GCC outputs:

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

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

1 个解决方案

#1


16  

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.

通过静态数组,我假设,你的意思是一个固定长度的数组(静态分配,如int数组[SIZE],不动态分配)。数组大小限制应取决于声明的数组的范围。

  • 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.
  • 如果gcc在linux上运行,则堆栈大小由某个环境变量决定。使用ulimit -a查看和ulimit -s STACK_SIZE以修改堆栈大小。

  • If gcc is running on windows (like MinGW), stack size can be specified by gcc -Wl,--stack, STACK_SIZE.
  • 如果gcc在Windows上运行(如MinGW),则可以通过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.
  • 如果已在全局范围内声明了数组,则该数组将存储在DATA或BSS部分中(基于数组是分别初始化还是未初始化)。 DATA和BSS部分大小由底层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.
  • 如果已在静态范围内声明了数组(如static int array [SIZE]),则再次将数组存储在DATA或BSS部分中(基于数组是分别初始化还是未初始化)。 DATA和BSS部分大小由底层OS确定。

#1


16  

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.

通过静态数组,我假设,你的意思是一个固定长度的数组(静态分配,如int数组[SIZE],不动态分配)。数组大小限制应取决于声明的数组的范围。

  • 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.
  • 如果gcc在linux上运行,则堆栈大小由某个环境变量决定。使用ulimit -a查看和ulimit -s STACK_SIZE以修改堆栈大小。

  • If gcc is running on windows (like MinGW), stack size can be specified by gcc -Wl,--stack, STACK_SIZE.
  • 如果gcc在Windows上运行(如MinGW),则可以通过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.
  • 如果已在全局范围内声明了数组,则该数组将存储在DATA或BSS部分中(基于数组是分别初始化还是未初始化)。 DATA和BSS部分大小由底层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.
  • 如果已在静态范围内声明了数组(如static int array [SIZE]),则再次将数组存储在DATA或BSS部分中(基于数组是分别初始化还是未初始化)。 DATA和BSS部分大小由底层OS确定。