- Sizeof
In the programming languages C and
C++ , theunary operator 'sizeof' is used to calculate the sizes of datatypes.sizeof
can be applied to all datatypes, be theyprimitive types such as the integer and floating-point types defined in the language, pointers to memory addresses, or the compound datatypes (unions, structs, or C++ classes) defined by the programmer.sizeof
is acompile-time operator that returns the size, in multiples of the size ofchar
, of the variable or parenthesized type-specifier that it precedes. The size ofchar
on most architectures is 1 byte (usually 8 bits) so for all practical purposes sizeof effectively returns the size in bytesNeed for
sizeof
In many programs, there are situations where it is useful to know the size of a particular datatype (one of the most common examples is
dynamic memory allocation using the library function
). Though for any givenmalloc implementation of C or C++ the size of a particular datatype is constant, the sizes of even primitive types in C and C++ are implementation defined (that is, not precisely defined by the standard). This can cause problems when trying to allocate a block of memory of the appropriate size. For example, say a programmer wants to allocate a block of memory big enough to hold tenvariables of typeint
. Because our hypothetical programmer doesn't know the exact size of typeint
, (s)he doesn't know how many bytes to askmalloc
for. Therefore, it is necessary to use the operatorsizeof
:In the preceding code, the programmer instructs
malloc
to allocate and return a pointer to memory. The size of the block allocated is equal to the number of bytes a single object of typeint
takes up, multiplied by 10, ensuring enough space for all 10int
s.It is generally not safe for a programmer to assume he or she knows the size of any datatype. For example, even though most implementations of C and C++ on
32-bit systems define typeint
to be 4 bytes, it is recommended by many programmers to always usesizeof
, as the size of anint
could change when code is ported to a different system, breaking the code. In addition, it is frequently very difficult to predict the sizes of compound datatypes such as astruct
orunion
due to structure "padding" (see "implementation" below). Another reason for using sizeof is readability, as this avoids magic numbers.Use
The 'sizeof' operator is used to determine the amount of space any data-element/datatype occupies in memory. To use
sizeof
, the keyword "sizeof
" is followed by a type name, variable, or expression. If a type name is used, it always needs to be enclosed in parentheses, whereas variable names and expressions can be specified with or without parentheses. Asizeof
expression evaluates to an unsigned value equal to the size in bytes of the "argument" datatype, variable, or expression (with datatypes,sizeof
evaluates to the size of the datatype; for variables and expressions it evaluates to the size of the type of the variable or expression). For example, assumingint
s are 4 bytes long, the following code will print 1,4:The value of a
sizeof
expression is always non-negative as the C standard specifies that the type of such an expression issize_t
, defined to be an unsigned integer type.Using
sizeof
with arraysWhen
sizeof
is applied to an array, the result is the size in bytes of the array in memory. The following program usessizeof
to determine the size of an array, avoiding a buffer overflow when copying characters:
Wikimedia Foundation. 2010.