The sizeof operator yields the size in bytes of the operand, which can be an expression or the parenthesized name of a type. A sizeof expression has the form:
>>-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.
The sizeof operator applied to a type name yields the amount of memory that would be used by an object of that type, including any internal or trailing padding. The size of any of the three kinds of char objects (unsigned, signed, or plain) is the size of a byte, 1. If the operand is a variable length array type, the operand is evaluated. The sizeof operator may not be applied to:
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, but does not evaluate it. 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.
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, or int or any enumeration type.
Types cannot be defined in a sizeof expression.
In the following example, the compiler is able to evaluate the size at compile time. The operand of sizeof, an expression, is not evaluated. The value of b is the integer constant 5, from initialization to the end of program run time:
#include <stdio.h> int main(void){ int b = 5; sizeof(b++); return 0; }
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 should use the sizeof operator to determine the size that a data type represents. For example:
sizeof(int);
The operand of the sizeof operator can be a vector type or the
result of dereferencing a pointer to vector type, provided that the AltiVec
language extensions have been 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.
The result of a sizeof expression depends on the type it is applied
to.
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. |
![]() | The result is always nonzero, and is equal to the number of bytes in an object of that class including any padding required for placing class objects in an array. |
![]() | The result is the size of the referenced object. |
Related References