Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

How large can a static array be in C?

Is there a maximum to the size of an array that can be declared statically in C?

for example

#define SIZE 1000000

int main()

{

double myarray[SIZE];

}

it doesn't generate an error, but if i use an array like this, the program exits and reports no errors but doesn't work.

I know I can declare dynamically to work around, but what is the maximum size allowed statically?

btw I'm using GCC 4.4.1-1ubuntu2 if it makes a difference.

Update:

It's strange, because I ran this on a high performance computer where I had 1GB per core, and it failed when I declared the arrays statically, but worked fine when I malloc'ed them.

3 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    You said declared statically, but myarray isn't static.

    Try:

    static double myarray[SIZE]; That will probably run.

    As already mentioned, there aren't fixed limits, but 1 meg isn't a big deal on a 32 or 64 bit desktop OS.

    Without static, myarray will be allocated on the stack. The maximum stack size is usually fairly small, well under 1 meg.

    With static, the memory will be allocated in your programs data segment. The OS will allocate this memory when the program loads, so the only real limit should be how much memory the OS is willing to allocate to one process.

  • 1 decade ago

    You ARE limited to the largest value permitted by a positive integer. Java is from the C-family of code. Java specifically limits data types. So...

    double myArray[ Integer.MAX_VALUE ]; is the largest theoretical size permitted. However, you will bang the heap's top end before you get to myArray[ 2147483647 ] = 3.14

    Array index calls and sets with a positive integer.

    It's been awhile since I coded C, but I do recall we used libraries or carefully defined the data types based upon the computer's architecture, so the maximum integer applies.

  • Silent
    Lv 7
    1 decade ago

    There's no limit in the language specification; the only limit is imposed by how much memory is available to your program, which is mostly determined by the operating system.

    Theoretically, you should be able to create objects of at least 32767 bytes in classic ANSI C, and up to 65535 bytes in C99. (Bear in mind that a double is usually 8 bytes.) Beyond that, it's all down to how much memory you have.

Still have questions? Get your answers by asking now.