What is the maximum size of static array that can be declared in GCC?

15,443

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.

  • 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.
Share:
15,443
ted
Author by

ted

Updated on July 22, 2022

Comments

  • ted
    ted almost 2 years

    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:

    Error "size of array 'array' is too large".
    
  • Z boson
    Z boson over 6 years
    It would best interesting to have some numbers (even if they can change) about DATA/BSS section size limits from Linux and Windows.
  • Peter Cordes
    Peter Cordes about 5 years
    GCC's max object size is also limited to PTRDIFF_MAX, so even though you can mmap more than 2GB in a 32-bit process, you can get undefined behaviour when using it.