Tuesday, September 1, 2009

sizeof

The sizeof operator returns the number of bytes required to store a variable or a data (1 byte = 8 bits). The operator can be applied to any type of variable:

sizeof( variable name );
sizeof( data_type_name );

sizeof is a handy tool in finding out the amount of elements in an array:

const int SIZE = sizeof( array ) / sizeof( element_type );
or
const int SIZE = sizeof( array ) / sizeof( array[ 0 ] );

When the value of size is always equal to the actual amount of elements in the array even if the size of the array is changed between complications.

Note that sizeof does not produce a correct answer if they array is a formal parameter of a function.

Reference: cppreference, space, msdn


Previous post

Next post


No comments:

Post a Comment