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.
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 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.
sizeof operator
with a fixed-point decimal type results in the total number of bytes
that are occupied by the decimal type. z/OS® XL
C/C++ implements
decimal data types using the native packed decimal format. Each digit
occupies half a byte. The sign occupies an additional half byte. The
following example gives you a result of 6 bytes: sizeof(decimal(10,2)); | 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. ![]() |
sizeof operator cannot be applied
to: - A bit field
- A function type
- An undefined structure or class
- An incomplete type (such as
void)
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.
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.- The operand of the
sizeof...operator must be an expression that names a parameter pack. - The operand of the
sizeofoperator cannot be an expression that names a parameter pack or a pack expansion.


A class