9

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

Example:

int array[0x8000000000000000]; 

For this line GCC outputs the following on a x86_64 bit system:

Error "size of array 'array' is too large".
34
  • 1
    Bus size is one factor. Virtual memory size can be another. 8-bit CPUs don't usually support virtual memory. Some 16-bit CPUs did, some did not. Most 32-bit CPUs do support virtual memory. I'll go out on a limb and claim that all 64-bit CPUs do support virtual memory, but someone will probably produce a counter-example. Commented Aug 22, 2013 at 4:47
  • 1
    @ted: you have to be clear where and how you are declaring the array. Global scope? Function scope? static keyword? Commented Aug 22, 2013 at 4:49
  • 1
    @JimBalter: Z80 CPUs (and 6502, 6800, 6809 and 8080 chips) were 8-bit machines but supported 16-bit addresses and hence 64 KiB (65536 bytes) of memory. Commented Aug 22, 2013 at 5:00
  • 1
    If your machine has 64 bytes of RAM and sizeof(int) == 2, then the maximum size of array you could use is int a[32]; ('size of memory / size of one element of an array') and then you'd not have any other variables available. But 64 bytes of memory should tell you that the maximum possible size would be 64, but each number would have to be a 1-byte number (signed char or unsigned char). In general, in the absence of virtual memory, if the memory size is M and the size of an item in the array is S, the maximum array size is M / S. Commented Aug 22, 2013 at 5:21
  • 1
    "No one is talking about the storage duration and Variable positioning other than you" -- You are incorrect ... the very first comment here says "block-scope array object with automatic storage duration". You are also extremely rude. Goodbye. Commented Aug 22, 2013 at 7:05

1 Answer 1

21

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.
Sign up to request clarification or add additional context in comments.

2 Comments

It would best interesting to have some numbers (even if they can change) about DATA/BSS section size limits from Linux and Windows.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.