Quest
Quest

Reputation: 2843

Size of dynamic array vs static array

What is the maximum size of static array, and dynamic array? I think that there is no limit for dynamic array but why static arrays have a limited size?

Upvotes: 0

Views: 475

Answers (2)

Piotr Praszmo
Piotr Praszmo

Reputation: 18320

Unhandled exception at 0x011164A7 in StackOverflow.exe: 0xC00000FD: Stack overflow (parameters: 0x00000000, 0x00482000)

This looks more like a runtime error. More precisely - stack overflow.

In most places the size of array is limited only by available memory. However, the limit on stack allocated objects is usually much more severe. By default, it's 1Mb on Windows and 8Mb on Linux. It looks like your array and other data already on the stack is taking more space than the limit.

There are few ways to avoid this error:

  1. Make this array static or declare it at top level of your module. This way it will be allocated in .bss segment instead of stack.
  2. Use malloc/new to explicitly allocate this array on heap.
  3. Use C++ collections such as std::vector instead of arrays.
  4. Increase stack size limit. On Linux this can be done with ulimit -s unlimited

Upvotes: 4

Am_I_Helpful
Am_I_Helpful

Reputation: 19168

The maximum size of an array is determined by the amount of memory that a program can access. On a 32-bit system, the maximum amount of memory that can be addressed by a pointer is 2^32 bytes which is 4 gigabytes. The actual limit may be less, depending on operating system implementation details.

Note that this has nothing to do with the amount of physical memory you have available. Even on a machine with substantially less than 1 GB of RAM, you can allocate a 2 GB array... it's just going to be slow, as most of the array will be in virtual memory, swapped out to disk.

Upvotes: 0

Related Questions