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".
TeeTrinker
  • 311
  • 2
  • 14
ted
  • 3,911
  • 3
  • 26
  • 49
  • This has little to nothing to do with GCC. If you mean a block-scope array object with automatic storage duration by "static array", then whether or not it fits into the memory depends on the stack size (assuming your architecture has a stack). –  Aug 22 '13 at 04:17
  • What I meant by static array is an array declared like this : int array[50] ? I am getting a "size of array is too large error" in Gcc in compile time? – ted Aug 22 '13 at 04:24
  • Yeah, that kind of array is called "a [ block-scope | file-scope] array object with automatic storage duration". If you get a compiler error, that's because GCC's being overzealous (not that it's bad!) and basically protects you from yourself. This does not, however, depend on the compiler, classically. –  Aug 22 '13 at 04:27
  • Are you compiling for a 32-bit or 64-bit executable? If you're compiling for 32-bits, you'll not be able to create a static object more than about 3.5 GiB in total. (4 GiB is the nominal maximum, but you lose some space to your program and the C library, etc.) For a 64-bit executable, there shouldn't really be a limit at compile time, but there will be at runtime; even with virtual memory, you need space on your disk system to back it up. Stack objects are more restricted, on both 32-bit and 64-bit systems. – Jonathan Leffler Aug 22 '13 at 04:30
  • @JonathanLeffler What will be that max value if I am compiling for a 8 bit architecture ? – ted Aug 22 '13 at 04:34
  • Hmmm...most 8-bit CPUs, AFAIK, use a 16-bit bus, so 64 KiB (65536 bytes). – Jonathan Leffler Aug 22 '13 at 04:36
  • So its basically depend on the size of bus.? – ted Aug 22 '13 at 04:42
  • 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. – Jonathan Leffler Aug 22 '13 at 04:47
  • *int array[50] ? I am getting a "size of array is too large error" in Gcc in compile time?* -- No, I don't think so. Show us the actual code and error message. – Jim Balter Aug 22 '13 at 04:49
  • 1
    @ted: you have to be clear *where* and *how* you are declaring the array. Global scope? Function scope? `static` keyword? – nneonneo Aug 22 '13 at 04:49
  • *If you mean a block-scope array object with automatic storage duration by "static array"* -- That's an auto array, not a static array. A static array has static storage duration. – Jim Balter Aug 22 '13 at 04:51
  • *What I meant by static array is an array declared like this : int array[50]* -- Declared *where*? If it's declared at file scope then it has static storage duration; otherwise it has automatic storage duration. – Jim Balter Aug 22 '13 at 04:52
  • *if I am compiling for a 8 bit architecture ?* -- You have a machine with only 256 bytes of memory? – Jim Balter Aug 22 '13 at 04:54
  • 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. – Jonathan Leffler Aug 22 '13 at 05:00
  • @JonathanLeffler This is just confused terminology. You referred to "32-bit and 64-bit systems" -- such systems of course have 8-bit bytes. In context, "8-bit system" would have to refer to a system with 8-bit ints and addresses. – Jim Balter Aug 22 '13 at 05:04
  • @JimBalter I have a machine with 64 byte of memory(RAM) (Check AVR attiny48 in atmel page)!!! And Compiler is not giving compiler error for int array[50]; its just another line to show what I meant bye static array (I don't know how to put newline in comments, Sorry for the confusion ) – ted Aug 22 '13 at 05:07
  • " I have a machine with 64 byte of memory(RAM) (Check AVR attiny48 in atmel page)!!!" -- And you didn't bother to put this little detail in your question? Thanks a lot for wasting people's time. – Jim Balter Aug 22 '13 at 05:09
  • Granted that there is confusion over the terminology, but the 8-bit CPUs cited primarily had 8-bit registers for arithmetic, with limited 16-bit register support. In the Z80, the A, B, C, D and E registers were 8-bit; the H and L registers separately were also 8-bit, but the HL register was a 16-bit register; the IX and IY registers were 16-bit. I've forgotten which other registers there were, but I'm sure [Wikipedia](http://en.wikipedia.org/wiki/Z80) would tell us. – Jonathan Leffler Aug 22 '13 at 05:11
  • @JimBalter What I need to know is how to determine the limit of size/value that we can specify in array sizes generally. I don't want to know about AVR architecture limit specifically!!!! – ted Aug 22 '13 at 05:13
  • You asked "What will be that max value if I am compiling for a 8 bit architecture ?" -- that's wanting to know about that limit specifically. But no one can tell you, because that architecture falls outside the minimum bounds required by the C standard. As for the general question, the C standard offers this footnote: "Implementations should avoid imposing fixed translation limits whenever possible." You won't get a better answer unless you ask a better question ... which means talking about your actual usage and requirements. – Jim Balter Aug 22 '13 at 05:19
  • 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. – Jonathan Leffler Aug 22 '13 at 05:21
  • @JimBalter My question was this : **What is the maximum size of static array that can be declared in GCC C compiler and how its determined ? Does this depend on the compiler/Architecture/Host system ?** And Jonathan explain me with 32/64 bit example So for simplifying/clarifying I asked with a 8 Bit example!!! – ted Aug 22 '13 at 05:27
  • Yes, I can read the question, there's no need to repeat it in bold. My comments still stand. – Jim Balter Aug 22 '13 at 05:30
  • @JimBalter I thought you didn't see the actualt question!!! Well you were commenting about storage duration, Where variable declarated. Did these actually has anything do with the Max size limit? If yes enlighten ME. – ted Aug 22 '13 at 05:37
  • "I thought you didn't see the actualt question!!!" -- I don't believe you. "Well you were commenting about storage duration" -- you referred to "static array" but your comments don't make it clear that you understand what that is. "Where variable declarated" -- yes, that's important. "Did these actually has anything do with the Max size limit? " -- Yes, very much so. " If yes enlighten ME." -- I and others have attempted to, but you don't seem interested in what we actually say. I'm done here ... good luck. – Jim Balter Aug 22 '13 at 06:45
  • One more comment, on your edit: Do you realize that you're asking for 2^23 terabytes of RAM? That's going to exceed numerous limits, probably including those imposed by the loader format, any one of which could trigger the gcc message. Stick to doing things that are sensible. – Jim Balter Aug 22 '13 at 06:58
  • @Jim Balter Others made sense and I up-voted those comments!!. What Didn't made sense is I asked you in my last question ? No one is talking about the storage duration and Variable positioning other than you, So if you are not wishing to answer don't participate. – ted Aug 22 '13 at 07:03
  • 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. – Jim Balter Aug 22 '13 at 07:05
  • @JimBalter Did i told you that I want to allocate that much of memory for an array. I just need to know how to determine that value where compile raise this error? – ted Aug 22 '13 at 07:05
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/35974/discussion-between-ted-and-jim-balter) – ted Aug 22 '13 at 07:07
  • @JimBalter Just read what he is wrote, I call these arrays (int array[20] = {};) static arrays, he was asking me am i talking about this **block-scope array object with automatic storage**. I didn't find anything that it has something to do with the Value ! – ted Aug 22 '13 at 07:14
  • One point : I compiled the array declaration inside the global scope and in function/local scope. Both times compiler is giving error at same array size value. Thats why i think variable position has to do nothing with it, Am i missing some thing? – ted Aug 22 '13 at 09:26
  • If you really want to keep any of this information around, it's best to put it into an answer. The comments have devolved into their own discussions. Comments are not meant for these discussions, that's why we have chat. – George Stocker Aug 22 '13 at 11:51
  • possible duplicate of [The maximum size of an array in C](http://stackoverflow.com/questions/9386979/the-maximum-size-of-an-array-in-c) – Ciro Santilli OurBigBook.com Aug 06 '15 at 18:56

1 Answers1

20

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.
zegkljan
  • 8,051
  • 5
  • 34
  • 49
Siddhartha Ghosh
  • 2,988
  • 5
  • 18
  • 25
  • 5
    It would best interesting to have some numbers (even if they can change) about DATA/BSS section size limits from Linux and Windows. – Z boson Aug 30 '17 at 09:04
  • 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. – Peter Cordes Mar 03 '19 at 21:05