sizeof
operatorGeneral topics | ||||||||||||||||
Flow control | ||||||||||||||||
Conditional execution statements | ||||||||||||||||
Iteration statements (loops) | ||||||||||||||||
Jump statements | ||||||||||||||||
Functions | ||||||||||||||||
Function declaration | ||||||||||||||||
Lambda function expression | ||||||||||||||||
inline specifier | ||||||||||||||||
Dynamic exception specifications(until C++17*) | ||||||||||||||||
noexcept specifier(C++11) | ||||||||||||||||
Exceptions | ||||||||||||||||
Namespaces | ||||||||||||||||
Types | ||||||||||||||||
Specifiers | ||||||||||||||||
| ||||||||||||||||
Storage duration specifiers | ||||||||||||||||
Initialization | ||||||||||||||||
Expressions | ||||||||||||||||
Alternative representations | ||||||||||||||||
Literals | ||||||||||||||||
Boolean -Integer -Floating-point | ||||||||||||||||
Character -String -nullptr(C++11) | ||||||||||||||||
User-defined(C++11) | ||||||||||||||||
Utilities | ||||||||||||||||
Attributes(C++11) | ||||||||||||||||
Types | ||||||||||||||||
typedef declaration | ||||||||||||||||
Type alias declaration(C++11) | ||||||||||||||||
Casts | ||||||||||||||||
Memory allocation | ||||||||||||||||
Classes | ||||||||||||||||
Class-specific function properties | ||||||||||||||||
| ||||||||||||||||
Special member functions | ||||||||||||||||
Templates | ||||||||||||||||
Miscellaneous | ||||||||||||||||
General | ||||
Literals | ||||
Operators | ||||
Conversions | ||||
Queries size of the object or type.
Used when actual size of the object must be known.
Contents |
sizeof( type) | (1) | ||||||||
sizeof expression
| (2) | ||||||||
type | - | atype-id (seetype naming) |
expression | - | an expression whoseoperator precedence is not lower thansizeof (e.g.sizeof a+ b is parsed as(sizeof a)+ b instead ofsizeof(a+ b)) |
The result of asizeof
expression is aconstant expression of typestd::size_t.
Depending on the computer architecture, abyte may consist of 8 or more bits, the exact number being recorded inCHAR_BIT.
The followingsizeof
expressions always evaluate to1:
| (since C++17) |
| (since C++20) |
sizeof
cannot be used with function types, incomplete types, or bit-fieldlvalues(until C++11)glvalues(since C++11).
When applied to a reference type, the result is the size of the referenced type.
When applied to a class type, the result is the number of bytes occupied by a complete object of that class, including any additional padding required to place such object in an array. The number of bytes occupied by apotentially-overlapping subobject may be less than the size of that object.
The result ofsizeof
is always nonzero, even if applied to an empty class type.
When applied to an expression,sizeof
doesnot evaluate the expression (i.e. the expression is an unevaluated operand)(since C++11), and even if the expression designates a polymorphic object, the result is the size of the static type of the expression. Lvalue-to-rvalue, array-to-pointer, or function-to-pointer conversions are not performed.Temporary materialization, however, is (formally) performed for prvalue arguments: the program is ill-formed if the argument is not destructible.(since C++17)
The example output corresponds to a system with 64-bit pointers and 32-bit int (a.k.a.LP64 orLLP64).
#include <cstdlib>#include <iostream> struct Empty{};struct Base{int a;};struct Derived: Base{int b;};struct Bit{unsigned bit:1;};struct CharChar{char c;char c2;};struct CharCharInt{char c;char c2;int i;};struct IntCharChar{int i;char c;char c2;};struct CharIntChar{char c;int i;char c2;};struct CharShortChar{char c;short s;char c2;}; int main(){ Empty e; Derived d; Base& b= d;[[maybe_unused]] Bit bit;int a[10]; auto f=[&](){return sizeof(int[10])== sizeof a?throw1: e;};// f(); // the return type is Empty, but always throws 1 auto println=[](auto rem,std::size_t size){std::cout<< rem<< size<<'\n';}; println("1) sizeof empty class: ", sizeof e); println("2) sizeof pointer: ", sizeof&e); println("3) sizeof(Bit) class: ", sizeof(Bit)); println("4) sizeof(int[10]) array of 10 int: ", sizeof(int[10])); println("5) sizeof a array of 10 int: ", sizeof a); println("6) length of array of 10 int: ",((sizeof a)/(sizeof*a))); println("7) length of array of 10 int (2): ",((sizeof a)/(sizeof a[0]))); println("8) sizeof the Derived class: ", sizeof d); println("9) sizeof the Derived through Base: ", sizeof b); println("A) sizeof(unsigned): ", sizeof(unsigned)); println("B) sizeof(int): ", sizeof(int)); println("C) sizeof(short): ", sizeof(short)); println("D) sizeof(char): ", sizeof(char)); println("E) sizeof(CharChar): ", sizeof(CharChar)); println("F) sizeof(CharCharInt): ", sizeof(CharCharInt)); println("G) sizeof(IntCharChar): ", sizeof(IntCharChar)); println("H) sizeof(CharIntChar): ", sizeof(CharIntChar)); println("I) sizeof(CharShortChar): ", sizeof(CharShortChar)); println("J) sizeof f(): ", sizeof f()); println("K) sizeof Base::a: ", sizeof Base::a); // println( "sizeof function: ", sizeof(void()) ); // error// println( "sizeof incomplete type: ", sizeof(int[]) ); // error// println( "sizeof bit-field: ", sizeof bit.bit ); // error}
Possible output:
1) sizeof empty class: 12) sizeof pointer: 83) sizeof(Bit) class: 44) sizeof(int[10]) array of 10 int: 405) sizeof a array of 10 int: 406) length of array of 10 int: 107) length of array of 10 int (2): 108) sizeof the Derived class: 89) sizeof the Derived through Base: 4A) sizeof(unsigned): 4B) sizeof(int): 4C) sizeof(short): 2D) sizeof(char): 1E) sizeof(CharChar): 2F) sizeof(CharCharInt): 8G) sizeof(IntCharChar): 8H) sizeof(CharIntChar): 12I) sizeof(CharShortChar): 6J) sizeof f(): 1K) sizeof Base::a: 4
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
CWG 1553 | C++11 | sizeof could be used with bit-field xvalues | prohibited |
alignof (C++11) | queries alignment requirements of a type (operator)[edit] |
sizeof... operator(C++11) | queries the number of elements in apack[edit] |
provides an interface to query properties of all fundamental numeric types (class template)[edit] | |
C documentation forsizeof |