The sizeof operator yields the size in bytes of the operand, which can be an expression or the parenthesized name of a type.
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.
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.
The
operand of the sizeof operator can be 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.
| 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. |
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. |
A reference |
The result is the size of the referenced object. |
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, or int or any enumeration type.
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.