What is the maximum number of elements an array can have

The maximum size of an array in C++ is not a static value, but a limit imposed by a number of factors. These include how you declare the array, the hardware memory limits, and any restrictions imposed by the compiler.

In the Arduino environment for AVR microcontrollers ( Uno, Mega,... ), the GCC compiler is used. The version Arduino utilizes has a maximum array size of 32KB - 1B. This value is far larger than the available SRAM on any AVR processor[1].

Below is a listing of the maximums for commonly used primitives using AVR bsaed Arduinos.

char a[ 32767 ];
short b[ 16383 ];
int c[ 16383 ];
long d[ 8191 ];
long long e[ 4095 ];
float f[ 8191 ];
double g[ 8191 ]; //double is the same size as float on AVR Arduino's

 As you can see different data types have different maximums. This is reflecting the 32KB maximum as 32767 bytes, not elements. On other compilers and platforms the upper memory limit may be greater, however every system has a limit on how much memory a single allocation can address. To calculate in code the number of elements an array has, this FAQ explains it in detail.

When using the Arduino DUE, the maximum addressable range increases to a whopping 2GB which means the compiler enforced maximum theoretically should never be reached. Here is the DUE's version of the above list.

char a[ 2147483647 ];
short b[ 1073741823 ];
int c[ 536870911 ];
long d[ 536870911 ];
long long e[ 268435455 ];
float f[ 536870911 ];
double g[ 268435455 ];

A handy but important tip to remember, is the maximum array size for any platform is also the maximum size an object can be ( class, struct, union ). This includes the combined usage of the objects data members and members of any derived objects. Hopefully this does not come as a surprise as C++ allows arrays of objects.

Tag Notes:

  1. Thereis an exception for processors like the ATMega 2560 which support external memory mapping. The Mega also has the room for large PROGMEM arrays, these are limited to the 32Kb range also.


Tags: array, C++
Last update:
2014-04-01 11:01
Christopher Andrews
Average rating: 3.3 (10 Votes)

You can comment this FAQ

Chuck Norris has counted to infinity. Twice.