The sizeof operator

The sizeof operator yields the size in bytes of the operand, which can be an expression or the parenthesized name of a type.

Read syntax diagramSkip visual syntax diagram
sizeof operator syntax

>>-sizeof--+-expr------------+---------------------------------><
           '-(--type-name--)-'   

The result for either kind of operand is not an lvalue, but a constant integer value. The type of the result is the unsigned integral type size_t defined in the header file stddef.h.

Except in preprocessor directives, you can use a sizeof expression wherever an integral constant is required. One of the most common uses for the sizeof operator is to determine the size of objects that are referred to during storage allocation, input, and output functions.

Another use of sizeof is in porting code across platforms. You can use the sizeof operator to determine the size that a data type represents. For example:
sizeof(int);

The sizeof operator applied to a type name yields the amount of memory that can be used by an object of that type, including any internal or trailing padding.

IBM extension begins The operand of the sizeof operator can be a vector variable, a vector type, or the result of dereferencing a pointer to vector type, provided that vector support is enabled. In these cases, the return value of sizeof is always 16.
vector bool int v1;
vector bool int *pv1 = &v1;
sizeof(v1); // vector type: 16.
sizeof(&v1); // address of vector: 4.
sizeof(*pv1); // dereferenced pointer to vector: 16.
sizeof(pv1); // pointer to vector: 4.
sizeof(vector bool int); // vector type: 16.
IBM extension ends
For compound types, results are as follows:
Operand Result
An array The result is the total number of bytes in the array. For example, in an array with 10 elements, the size is equal to 10 times the size of a single element. The compiler does not convert the array to a pointer before evaluating the expression.
C++ only A class The result is always nonzero. It is equal to the number of bytes in an object of that class, also including any padding required for placing class objects in an array.
C++ A reference The result is the size of the referenced object.
The sizeof operator cannot be applied to:
  • A bit field
  • A function type
  • An undefined structure or class
  • An incomplete type (such as void)
The sizeof operator applied to an expression yields the same result as if it had been applied to only the name of the type of the expression. At compile time, the compiler analyzes the expression to determine its type. None of the usual type conversions that occur in the type analysis of the expression are directly attributable to the sizeof operator. However, if the operand contains operators that perform conversions, the compiler does take these conversions into consideration in determining the type. For example, the second line of the following sample causes the usual arithmetic conversions to be performed. Assuming that a short uses 2 bytes of storage and an int uses 4 bytes,
short x; … sizeof (x)         /* the value of sizeof operator is 2 */
short x; … sizeof (x + 1)     /* value is 4, result of addition is type int */
The result of the expression x + 1 has type int and is equivalent to sizeof(int). The value is also 4 if x has type char, short, int, or any enumeration typeof the default enum size.

A variable length array can be the operand of a sizeof expression. In this case, the operand is evaluated at run time, and the size is neither an integer constant nor a constant expression, even though the size of each instance of a variable array does not change during its lifetime.

C++11 begins sizeof... is a unary expression operator introduced by the variadic template feature. This operator accepts an expression that names a parameter pack as its operand. It then expands the parameter pack and returns the number of arguments provided for the parameter pack. Consider the following example:
template<typename...T> void foo(T...args){
    int v = sizeof...(args);
}
In this example, the variable v is assigned to the number of the arguments provided for the parameter pack args.
Notes:
  • The operand of the sizeof... operator must be an expression that names a parameter pack.
  • The operand of the sizeof operator cannot be an expression that names a parameter pack or a pack expansion.
For more information, see Variadic templates (C++11)
C++11 ends