Getting a grip of the sizeof() utility

I was trying to make heads and tails of how to effectively use the sizeof() utility. After some reading and some coding I wrote this code to better understand using sizeof() in programming. This was written on a UNO board so in the comments the byte sizes might return a different value.

short array1[5]; 
/*
sizeof() will return a value that is the combination of bytes 
of the declared data type times the index number.
Changing the data type will change the sizeof() value 
*/
int array2[] = {64,22,4,28,32,111,2}; 
int arrayCount = sizeof(array2)/2; 
/*
Add elements to the array will change the sizeof() value
This number, just like above, is a combination of the index number 
(or number of elements in the array) and the data type. For this 
array keep the data type as interger (2 bytes on the UNO).
ArrayCount formats the number of elements in the array
You can use this to format an array no matter how many 
elements you add. 
*/


void setup(){
Serial.begin(9600); 
delay(1000);  

// One byte data types 
Serial.print("size of boolean "); 
Serial.println(sizeof(boolean));
Serial.print("Size of byte "); 
Serial.println(sizeof(byte)); 
Serial.print("Size of Char ");
Serial.println(sizeof(char)); 

// Two byte data type 
Serial.print ("size of interger "); 
Serial.println (sizeof(int));
Serial.print("Size of word ");
Serial.println(sizeof(word)); 
Serial.print("Size of short ");
Serial.println(sizeof(short));

// Four byte data type 
Serial.print("Size of Long "); 
Serial.println(sizeof(long)); 
Serial.print("Size of float "); 
Serial.println(sizeof(float)); 
Serial.print("Size of double ");
Serial.println(sizeof(double)); 

// See how sizeof functions with arrays 
Serial.print("Size of array1 ");
Serial.println(sizeof(array1));
//see what happens when you change the index number 
//see what happens when you change the data type.
Serial.print("Size of array2 ");
Serial.println(sizeof(array2));

//see what happens when you add more numbers to the array
Serial.print("There are "); 
Serial.print(arrayCount); 
Serial.println(" elements in array2"); 

}
void loop(){

}

Did you have a programming question?

One fundamental thing you will run into is when arrays get converted to pointers. In the C/C++ languages, arrays are not first class data types. When you pass an array to a function, it does not copy the whole array, instead it passes a pointer to the array. If you do sizeof that pointer, it will return the size of a single pointer instead of the array.

For example, consider this program (I'm not going to actually fire up an Arduino to get the output):

int array[10];

void pass_array (int parray[])
{
    Serial.print ("sizeof (parray) = ");
    Serial.println (sizeof (parray));
}

void startup (void)
{
    Serial.begin (9600);
    Serial.print ("sizeof (array) = ");
    Serial.println (sizeof (array));
    pass_array (array);
}

void loop (void)
{
    // just loop
}

It should print on an AVR arduino (i.e. not Due):

sizeof (array) = 20
sizeof (parray) = 2

Results in:

sizeof (array) = 20
sizeof (parray) = 2

Lefty

Another thing you might come across and find confusing is that 'sizeof' is not a function, it is an operator. The parentheses are optional:

    int i;
    const int size_of_i = sizeof i;

Thanks guys- I ran across the sizeof operator in a book, which didn't really cover what it did and how it worked. I've added the extra examples to my code :)

And an important thing to realise is that sizeof is computed at compile-time, not when the program is running, so each particular instance of sizeof will return an unchanging value each time the program encounters it. It can be applied to a type specifier or a variable (in which case the compile-time type of the variable is used). Sizes for function-types are not defined unless it's a pointer to a function.

This might be what you are looking for:

#define membersof(array) (sizeof(array)/sizeof(array[0]))

The C/C++ language does not provide a predefined keyword for the number of elements in a static vector/array. The above macro is a very common pattern in code. The below naming convention is also very common (ie suffix _MAX):

#define ARRAY_MAX 42
// or 
const int ARRAY_MAX = 42;
// and then
float array[ARRAY_MAX];
...
for (int i = 0; i < ARRAY_MAX; i++) array[i] = ...

Cheers!