An array of "anything" occupies a number of contiguous memory locations. The memory location associated with address of an element of an array of "anything"s can hold the value of a variable of type "anything."
For numeric data types:
Standard C++ has built-in integer data types of char
, short int
and long int
. There are built-in floating point data types float
. For each of the integer data types, there is a corresponding unsigned variety. Variables of unsigned data types occupy the same amount of memory as their signed counterparts.
Arduino has a typedef named "byte
" which is a C++ unsigned char
. That is, the Arduino byte
is not a new or different data type; it is just another name for a standard C++ unsigned char
Now, the C++ standard does not require that ints, long ints, etc. have any specific lengths. For a given C++ compiler, the sizeof()
operator returns how many bytes of memory will be occupied by a given variable of a given data type. The C++ standard does specify that sizeof(char) always returns 1. I think that most computer architectures these days use one byte of memory to store a char. They didn't have C or C++ back in the days we were running FORTRAN programs on CDC6600 computers (which had a memory granularity of 60 bits, not today's puny eight bits).
The C++ standard also specifies that the size of a short int
is greater than or equal to the size of a char
, the size of an int
is greater than or equal to the size of a short int
, and the size of a long int
is greater than or equal to the size of an int.
For avr-gcc, which is Arduino's compiler, sizeof(int)
returns a value of 2 and sizeof(long)
returns a value of 4. So---an int
takes two bytes and a long int
takes four bytes.
If you have an array of, say, 10 ints
, then with avr-gcc, the array takes up 20 bytes. For compilers that you are likely to run across on your desktop or laptop workstation these days, sizeof(int)
will probably return a value of 4
(but that's not guaranteed). For example, on my Linux computer, an array of 10 ints
takes up 40 bytes of memory.
Bottom line: No matter what kind of C++ array you have, the number of elements in the array can be calculated (at compile time) by dividing the sizeof the array by the sizeof an element of the array. The code that calculates the number of elements in an array of a given data type is portable to other compilers even if the sizes of the particular data types are different.
number_of_elements_in_array = sizeof(array) / sizeof(array);
The current Standard C++ language definition does not allow variable-length arrays. However, many C++ compilers do allow this (they got it from the current ISO C language standard). If a compiler does allow this, then evaluations of expressions containing the sizeof
operator for a variable-length array are done at run time, not compile time. Since I am often interested in portability to other compilers, I usually eschew non-standard language features for routine calculations, so I rarely use variable-length arrays. But that's just me. I'm funny that way.