HOME

TheInfoList



OR:

sizeof is a
unary operator In mathematics, an unary operation is an operation with only one operand, i.e. a single input. This is in contrast to binary operations, which use two operands. An example is any function , where is a set. The function is a unary operation ...
in the programming languages C and
C++ C, or c, is the third letter in the Latin alphabet, used in the modern English alphabet, the alphabets of other western European languages and others worldwide. Its name in English is ''cee'' (pronounced ), plural ''cees''. History "C" ...
. It generates the storage size of an expression or a
data type In computer science and computer programming, a data type (or simply type) is a set of possible values and a set of allowed operations on it. A data type tells the compiler or interpreter how the programmer intends to use the data. Most prog ...
, measured in the number of ''char''-sized units. Consequently, the construct ''sizeof (char)'' is guaranteed to be ''1''. The actual number of
bit The bit is the most basic unit of information in computing and digital communications. The name is a portmanteau of binary digit. The bit represents a logical state with one of two possible values. These values are most commonly represented ...
s of type char is specified by the preprocessor macro , defined in the standard
include file Many programming languages and other computer files have a directive, often called include (sometimes copy or import), that causes the contents of the specified file to be inserted into the original file. These included files are called copybooks ...
limits.h In the C programming language, data types constitute the semantics and characteristics of storage of data elements. They are expressed in the language syntax in form of declarations for memory locations or variables. Data types also determin ...
. On most modern computing platforms this is eight bits. The result of ''sizeof'' has an unsigned integer type that is usually denoted by
size_t Size in general is the magnitude or dimensions of a thing. More specifically, ''geometrical size'' (or ''spatial size'') can refer to linear dimensions (length, width, height, diameter, perimeter), area, or volume. Size can also be measur ...
. The operator has a single operand, which is either an expression or the cast of a data type, which is a data type enclosed in parentheses. Data types may not only be
primitive types In computer science, primitive data types are a set of basic data types from which all other data types are constructed. Specifically it often refers to the limited set of data representations in use by a particular processor, which all compiled p ...
, such as
integer An integer is the number zero (), a positive natural number (, , , etc.) or a negative integer with a minus sign (−1, −2, −3, etc.). The negative numbers are the additive inverses of the corresponding positive numbers. In the language o ...
and
floating-point In computing, floating-point arithmetic (FP) is arithmetic that represents real numbers approximately, using an integer with a fixed precision, called the significand, scaled by an integer exponent of a fixed base. For example, 12.345 can be r ...
types, but also pointer types, and compound datatypes ( unions,
struct In computer science, a record (also called a structure, struct, or compound data) is a basic data structure. Records in a database or spreadsheet are usually called " rows". A record is a collection of ''fields'', possibly of different data ...
s, and C++ classes).


Purpose

Many programs must know the storage size of a particular datatype. Though for any given
implementation Implementation is the realization of an application, or execution of a plan, idea, model, design, specification, standard, algorithm, or policy. Industry-specific definitions Computer science In computer science, an implementation is a real ...
of C or C++ the size of a particular datatype is constant, the sizes of even primitive types in C and C++ may be defined differently for different platforms of implementation. For example, runtime allocation of array space may use the following code, in which the sizeof operator is applied to the cast of the type ''int'': int *pointer = malloc(10 * sizeof (int)); In this example, function ''malloc'' allocates memory and returns a pointer to the memory block. The size of the block allocated is equal to the number of bytes for a single object of type ''int'' multiplied by 10, providing space for ten integers. It is generally not safe to assume the size of any datatype. For example, even though most implementations of C and C++ on
32-bit In computer architecture, 32-bit computing refers to computer systems with a processor, memory, and other major system components that operate on data in 32-bit units. Compared to smaller bit widths, 32-bit computers can perform large calculati ...
systems define type ''int'' to be four octets, this size may change when code is
ported In software engineering, porting is the process of adapting software for the purpose of achieving some form of execution in a computing environment that is different from the one that a given program (meant for such execution) was originally desi ...
to a different system, breaking the code. The exception to this is the data type ''char'', which always has the size ''1'' in any standards-compliant C implementation. In addition, it is frequently difficult to predict the sizes of compound datatypes such as a ''struct'' or ''union'', due to padding. The use of ''sizeof'' enhances readability, since it avoids unnamed numeric constants ( magic numbers). An equivalent syntax for allocating the same array space results from using the dereferenced form of the pointer to the storage address, this time applying the operator to a pointer variable: int *pointer = malloc(10 * sizeof *pointer);


Use

The operator ''sizeof'' produces the required memory storage space of its operand when the code is compiled. The operand is written following the keyword ''sizeof'' and may be the symbol of a storage space, e.g., a variable, an
expression Expression may refer to: Linguistics * Expression (linguistics), a word, phrase, or sentence * Fixed expression, a form of words with a specific meaning * Idiom, a type of fixed expression * Metaphorical expression, a particular word, phrase, ...
, or a type cast. The latter is a type name enclosed in parentheses. The result of the operator is the size of the operand in bytes, or the size of the memory storage requirement. For expressions, it evaluates to the representation size for the type that would result from evaluation of the expression, which is not performed. For example, since ''sizeof (char)'' is defined to be 1 and assuming the integer type is four bytes long, the following code fragment prints :
char c;
printf ("%zu,%zu\n", sizeof c, sizeof (int));
Certain standard header files, such as ''stddef.h'', define ''
size_t Size in general is the magnitude or dimensions of a thing. More specifically, ''geometrical size'' (or ''spatial size'') can refer to linear dimensions (length, width, height, diameter, perimeter), area, or volume. Size can also be measur ...
'' to denote the unsigned integral type of the result of a ''sizeof'' expression. The ''printf'' width specifier ''z'' is intended to format that type. ''sizeof'' cannot be used in
C preprocessor The C preprocessor is the macro preprocessor for the C, Objective-C and C++ computer programming languages. The preprocessor provides the ability for the inclusion of header files, macro expansions, conditional compilation, and line control ...
expressions, such as , because it is an element of the programming language, not of the preprocessor syntax, which has no data types. The following example in C++ uses the operator ''sizeof'' with variadic templates.
template 
std::size_t GetSize (Args&&... args)

''sizeof'' can be used with variadic templates in C++11 and above on a parameter pack to determine the number of arguments.


Application to arrays

When ''sizeof'' is applied to the name of an array, the result is the number of bytes required to store the entire array. This is one of the few exceptions to the rule that the name of an array is converted to a pointer to the first element of the array, and is possible just because the actual array size is fixed and known at compile time, when the ''sizeof'' operator is evaluated. The following program uses ''sizeof'' to determine the size of a declared array, avoiding a
buffer overflow In information security and programming, a buffer overflow, or buffer overrun, is an anomaly whereby a program, while writing data to a buffer, overruns the buffer's boundary and overwrites adjacent memory locations. Buffers are areas of memor ...
when copying characters: #include int main(int argc, char **argv) Here, is equivalent to , which evaluates to 10, because the size of the type ''char'' is defined as 1.
C99 C99 (previously known as C9X) is an informal name for ISO/IEC 9899:1999, a past version of the C programming language standard. It extends the previous version ( C90) with new features for the language and the standard library, and helps impl ...
adds support for flexible array members to structures. This form of array declaration is allowed as the last element in structures only, and differs from normal arrays in that no length is specified to the compiler. For a structure named ''s'' containing a flexible array member named ''a'', is therefore equivalent to : #include struct flexarray ; int main(int argc, char **argv) In this case the ''sizeof'' operator returns the size of the structure, including any padding, but without any storage allowed for the array. Most platforms produce the following output: :
C99 C99 (previously known as C9X) is an informal name for ISO/IEC 9899:1999, a past version of the C programming language standard. It extends the previous version ( C90) with new features for the language and the standard library, and helps impl ...
also allows variable length arrays that have the length specified at runtime, although the feature is considered an optional implementation in later versions of the C standard. In such cases, the ''sizeof'' operator is evaluated in part at runtime to determine the storage occupied by the array.
#include 

size_t flexsize(int n)


int main(void)

''sizeof'' can be used to determine the number of elements in an array, by dividing the size of the entire array by the size of a single element:
int main (void)


Incomplete types

''sizeof'' can only be applied to "completely" defined types. With arrays, this means that the dimensions of the array must be present in its
declaration Declaration may refer to: Arts, entertainment, and media Literature * ''Declaration'' (book), a self-published electronic pamphlet by Michael Hardt and Antonio Negri * ''The Declaration'' (novel), a 2008 children's novel by Gemma Malley Music ...
, and that the type of the elements must be completely defined. For ''struct''s and ''union''s, this means that there must be a member list of completely defined types. For example, consider the following two source files:
/* file1.c */
int arr  0
struct x ;
/* more code */

/* file2.c */
extern int arr [];
struct x;
/* more code */
Both files are perfectly legal C, and code in can apply ''sizeof'' to ''arr'' and . However, it is illegal for code in to do this, because the definitions in are not complete. In the case of ''arr'', the code does not specify the dimension of the array; without this information, the compiler has no way of knowing how many elements are in the array, and cannot calculate the array's overall size. Likewise, the compiler cannot calculate the size of because it does not know what members it is made up of, and therefore cannot calculate the sum of the sizes of the structure's members (and padding). If the programmer provided the size of the array in its declaration in , or completed the definition of by supplying a member list, this would allow the application of ''sizeof'' to ''arr'' or in that source file.


Object members

C++11 introduced the possibility to apply the ''sizeof'' parameter to specific members of a class without the necessity to instantiate the object to achieve this. The following example for instance yields and on most platforms.
#include 

struct foo ;

int main ()


Variadic template packs

C++11 introduced
variadic template In computer programming, variadic templates are templates that take a variable number of arguments. Variadic templates are supported by C++ (since the C++11 standard), and the D programming language. C++ The variadic template feature of C++ wa ...
s; the keyword ''sizeof'' followed by
ellipsis The ellipsis (, also known informally as dot dot dot) is a series of dots that indicates an intentional omission of a word, sentence, or whole section from a text without altering its original meaning. The plural is ellipses. The term origin ...
returns the number of elements in a parameter pack.
template 
void print_size (Args... args)


int main ()


Implementation

When applied to a fixed-length datatype or variable, expressions with the operator ''sizeof'' are evaluated during program compilation; they are replaced by constant result-values. The
C99 C99 (previously known as C9X) is an informal name for ISO/IEC 9899:1999, a past version of the C programming language standard. It extends the previous version ( C90) with new features for the language and the standard library, and helps impl ...
standard introduced
variable-length array In computer programming, a variable-length array (VLA), also called variable-sized or runtime-sized, is an array data structure whose length is determined at run time (instead of at compile time). In C, the VLA is said to have a variably modified t ...
s (VLAs), which required evaluation for such expressions during program execution. In many cases, the implementation specifics may be documented in an
application binary interface In computer software, an application binary interface (ABI) is an interface between two binary program modules. Often, one of these modules is a library or operating system facility, and the other is a program that is being run by a user. An ' ...
(ABI) document for the platform, specifying formats, padding, and alignment for the data types, to which the compiler must conform.


Structure padding

When calculating the size of any object type, the compiler must take into account any required
data structure alignment Data structure alignment is the way data is arranged and accessed in computer memory. It consists of three separate but related issues: data alignment, data structure padding, and packing. The CPU in modern computer hardware performs reads and ...
to meet efficiency or architectural constraints. Many computer architectures do not support multiple-byte access starting at any byte address that is not a multiple of the word size, and even when the architecture allows it, usually the processor can fetch a word-aligned object faster than it can fetch an object that straddles multiple words in memory. Therefore, compilers usually align data structures to at least a
word A word is a basic element of language that carries an objective or practical meaning, can be used on its own, and is uninterruptible. Despite the fact that language speakers often have an intuitive grasp of what a word is, there is no consen ...
boundary, and also align individual members to their respective boundaries. In the following example, the structure ''student'' is likely to be aligned on a word boundary, which is also where the member ''grade'' begins, and the member ''age'' is likely to start at the next word address. The compiler accomplishes the latter by inserting padding bytes between members as needed to satisfy the alignment requirements. There may also be padding at the end of a structure to ensure proper alignment in case the structure is used as an element of an array. Thus, the aggregate size of a structure in C can be greater than the sum of the sizes of its individual members. For example, on many systems the following code prints :
struct student ;

printf ("%zu", sizeof (struct student));


See also

*
typeof typeof, alternately also typeOf, and TypeOf, is an operator provided by several programming languages to determine the data type of a variable. This is useful when constructing programs that must accept multiple types of data without explicitly s ...
*
offsetof C's macro is an ANSI C library feature found in . It evaluates to the offset (in bytes) of a given member within a struct or union type, an expression of type . The offsetof() macro takes two parameters, the first being a structure name, and th ...


References

{{reflist C (programming language) C++ Articles with example C code Operators (programming) Unary operations