Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/language/sizeof"

From cppreference.com
< cpp‎ | language
(Reverted the example: The example in this page should demonstrate the nature and usage of sizeof rather than other features. Added a simple case to demonstrate the non-evaluating nature of sizeof.)
m (Notes: fix spacing)
 
(10 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{title|sizeof operator}}
+
{{title|{{tt|sizeof}} operator}}
 
{{cpp/language/expressions/navbar}}
 
{{cpp/language/expressions/navbar}}
 
Queries size of the object or type.
 
Queries size of the object or type.
Line 11: Line 11:
 
{{sdsc end}}
 
{{sdsc end}}
  
Both versions are constant expressions of type {{lc|std::size_t}}.  
+
Both versions are constant expressions of type {{lc|std::size_t}}.
 +
 
 +
{{par begin}}
 +
{{par|{{spar|type}}|a {{spar|type-id}} (see {{rlp|type#Type naming|type naming}})}}
 +
{{par|{{spar|expression}}|an expression whose {{rlp|operator precedence}} is not lower than {{tt|sizeof}} (e.g. {{c|sizeof a + b}} is parsed as {{c|(sizeof a) + b}} instead of {{c|sizeof (a + b)}})}}
 +
{{par end}}
  
 
===Explanation===
 
===Explanation===
Line 18: Line 23:
  
 
===Notes===
 
===Notes===
Depending on the computer architecture, a [[enwiki:Byte|byte]] may consist of 8 ''or more'' bits, the exact number being recorded in {{lc|CHAR_BIT}}.
+
Depending on the computer architecture, a {{enwiki|byte}} may consist of 8 ''or more'' bits, the exact number being recorded in {{lc|CHAR_BIT}}.
  
 
The following {{tt|sizeof}} expressions always evaluate to {{c|1}}:
 
The following {{tt|sizeof}} expressions always evaluate to {{c|1}}:
Line 24: Line 29:
 
* {{c|sizeof(signed char)}}
 
* {{c|sizeof(signed char)}}
 
* {{c|sizeof(unsigned char)}}
 
* {{c|sizeof(unsigned char)}}
{{rrev|since=c++17|* {{c|sizeof(std::byte)}}}}
+
{{rev begin}}
{{rrev|since=c++20|* {{c|sizeof(char8_t)}}}}
+
{{rev|since=c++17|
 +
* {{c|sizeof(std::byte)}}
 +
}}
 +
{{rev|since=c++20|
 +
* {{c|sizeof(char8_t)}}
 +
}}
 +
{{rev end}}
  
 
{{tt|sizeof}} cannot be used with function types, incomplete types, or bit-field {{rev inl|until=c++11|lvalues}}{{rev inl|since=c++11|glvalues}}.
 
{{tt|sizeof}} cannot be used with function types, incomplete types, or bit-field {{rev inl|until=c++11|lvalues}}{{rev inl|since=c++11|glvalues}}.
Line 35: Line 46:
 
The result of {{tt|sizeof}} is always nonzero, even if applied to an empty class type.
 
The result of {{tt|sizeof}} is always nonzero, even if applied to an empty class type.
  
When applied to an expression, {{tt|sizeof}} does {{rlp|expressions#Unevaluated expressions|not evaluate the expression}}, 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. {{rev inl|since=c++17|{{rlp|implicit conversion#Temporary materialization|Temporary materialization}}, however, is (formally) performed for prvalue arguments: the program is ill-formed if the argument is not destructible.}}
+
When applied to an expression, {{tt|sizeof}} does {{rlp|expressions#Potentially-evaluated expressions|not evaluate the expression}}{{rev inl|since=c++11| (i.e. the expression is an unevaluated operand)}}, 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.{{rev inl|since=c++17| {{rlp|implicit conversion#Temporary materialization|Temporary materialization}}, however, is (formally) performed for prvalue arguments: the program is ill-formed if the argument is not destructible.}}
  
 
===Keywords===
 
===Keywords===
Line 42: Line 53:
 
===Example===
 
===Example===
 
{{example
 
{{example
|The example output corresponds to a system with 64-bit pointers and 32-bit int.
+
|The example output corresponds to a system with 64-bit pointers and 32-bit int (a.k.a. {{rlp|types#Data models|'''LP64''' or '''LLP64'''}}).
 
|code=
 
|code=
#include <iostream>
 
 
#include <cstdlib>
 
#include <cstdlib>
+
#include <iostream>
struct Empty {};
+
 
struct Base { int a; };
+
struct Empty         { };
 +
struct Base           { int a; };
 
struct Derived : Base { int b; };
 
struct Derived : Base { int b; };
struct Bit { unsigned bit: 1; };
+
struct Bit           { unsigned bit: 1; };
struct CharChar     { char c; char c2; };
+
struct CharChar       { char c; char c2; };
struct CharCharInt   { char c; char c2; int i; };
+
struct CharCharInt   { char c; char c2; int i; };
struct IntCharChar   { int i;  char c;  char c2; };
+
struct IntCharChar   { int i;  char c;  char c2; };
struct CharIntChar   { char c; int i;  char c2; };
+
struct CharIntChar   { char c; int i;  char c2; };
struct CharShortChar { char c; short s; char c2; };
+
struct CharShortChar { char c; short s; char c2; };
+
 
 
int main()
 
int main()
 
{
 
{
Line 67: Line 78:
 
     auto f = [&]() { return sizeof(int[10]) == sizeof a ? throw 1 : e; };
 
     auto f = [&]() { return sizeof(int[10]) == sizeof a ? throw 1 : e; };
 
//  f(); // the return type is Empty, but always throws 1
 
//  f(); // the return type is Empty, but always throws 1
+
   
     std::cout  
+
     auto println = [](auto rem, std::size_t size) { std::cout << rem << size << '\n'; };
      << "1) sizeof empty class:              " << sizeof e         << '\n'
+
   
      << "2) sizeof pointer:                  " << sizeof &e       << '\n'
+
    println( "1) sizeof empty class:              ", sizeof e                     );
//    << "3) sizeof function:                " << sizeof(void())  << '\n' // error
+
    println( "2) sizeof pointer:                  ", sizeof &e                   );
//    << "4) sizeof incomplete type:          " << sizeof(int[])    << '\n' // error
+
    println( "3) sizeof(Bit) class:              ", sizeof(Bit)                 );
//    << "5) sizeof bit field:                " << sizeof bit.bit  << '\n' // error
+
    println( "4) sizeof(int[10]) array of 10 int: ", sizeof(int[10])             );
      << "6) sizeof(Bit) class:              " << sizeof(Bit)     << '\n'
+
    println( "5) sizeof a        array of 10 int: ", sizeof a                     );
      << "7) sizeof(int[10]) array of 10 int: " << sizeof(int[10]) << '\n'
+
    println( "6) length of array of 10 int:      ", ((sizeof a) / (sizeof *a))  );
      << "8) sizeof a        array of 10 int: " << sizeof a         << '\n'
+
    println( "7) length of array of 10 int (2):  ", ((sizeof a) / (sizeof a[0])) );
      << "9) length of array of 10 int:      " << ((sizeof a) / (sizeof *a))  << '\n'
+
    println( "8) sizeof the Derived class:        ", sizeof d                     );
      << "A) length of array of 10 int (2):  " << ((sizeof a) / (sizeof a[0])) << '\n'
+
    println( "9) sizeof the Derived through Base: ", sizeof b                     );
      << "B) sizeof the Derived class:        " << sizeof d         << '\n'
+
    println( "A) sizeof(unsigned):                ", sizeof(unsigned)             );
      << "C) sizeof the Derived through Base: " << sizeof b         << '\n'
+
    println( "B) sizeof(int):                    ", sizeof(int)                 );
      << "D) sizeof(unsigned)                 " << sizeof(unsigned) << '\n'
+
    println( "C) sizeof(short):                  ", sizeof(short)               );
      << "E) sizeof(int)                     " << sizeof(int)     << '\n'
+
    println( "D) sizeof(char):                    ", sizeof(char)                 );
      << "F) sizeof(short)                   " << sizeof(short)   << '\n'
+
     println( "E) sizeof(CharChar):                ", sizeof(CharChar)             );
      << "G) sizeof(char)                     " << sizeof(char)    << '\n'
+
    println( "F) sizeof(CharCharInt):            ", sizeof(CharCharInt)         );
      << "H) sizeof(CharChar)                 " << sizeof(CharChar) << '\n'
+
    println( "G) sizeof(IntCharChar):            ", sizeof(IntCharChar)         );
      << "I) sizeof(CharCharInt)             " << sizeof(CharCharInt) << '\n'
+
    println( "H) sizeof(CharIntChar):            ", sizeof(CharIntChar)         );
      << "J) sizeof(IntCharChar)             " << sizeof(IntCharChar) << '\n'
+
    println( "I) sizeof(CharShortChar):          ", sizeof(CharShortChar)       );
      << "K) sizeof(CharIntChar)             " << sizeof(CharIntChar) << '\n'
+
    println( "J) sizeof f():                      ", sizeof f()                   );
      << "L) sizeof(CharShortChar)           " << sizeof(CharShortChar) << '\n'
+
    println( "K) sizeof Base::a:                  ", sizeof Base::a              );
      << "M) sizeof f():                      " << sizeof f() << '\n';
+
 
 +
//  println( "sizeof function:        ", sizeof(void()) ); // error
 +
//  println( "sizeof incomplete type: ", sizeof(int[])  ); // error
 +
//  println( "sizeof bit-field:      ", sizeof bit.bit ); // error
 
}
 
}
 
|p=true
 
|p=true
Line 96: Line 110:
 
1) sizeof empty class:              1
 
1) sizeof empty class:              1
 
2) sizeof pointer:                  8
 
2) sizeof pointer:                  8
6) sizeof(Bit) class:              4
+
3) sizeof(Bit) class:              4
7) sizeof(int[10]) array of 10 int: 40
+
4) sizeof(int[10]) array of 10 int: 40
8) sizeof a        array of 10 int: 40
+
5) sizeof a        array of 10 int: 40
9) length of array of 10 int:      10
+
6) length of array of 10 int:      10
A) length of array of 10 int (2):  10
+
7) length of array of 10 int (2):  10
B) sizeof the Derived class:        8
+
8) sizeof the Derived class:        8
C) sizeof the Derived through Base: 4
+
9) sizeof the Derived through Base: 4
D) sizeof(unsigned)                 4
+
A) sizeof(unsigned):                4
E) sizeof(int)                     4
+
B) sizeof(int):                    4
F) sizeof(short)                   2
+
C) sizeof(short):                  2
G) sizeof(char)                     1
+
D) sizeof(char):                    1
H) sizeof(CharChar)                 2
+
E) sizeof(CharChar):                2
I) sizeof(CharCharInt)             8
+
F) sizeof(CharCharInt):            8
J) sizeof(IntCharChar)             8
+
G) sizeof(IntCharChar):            8
K) sizeof(CharIntChar)             12
+
H) sizeof(CharIntChar):            12
L) sizeof(CharShortChar)           6
+
I) sizeof(CharShortChar):          6
M) sizeof f():                      1
+
J) sizeof f():                      1
 +
K) sizeof Base::a:                  4
 +
 
 
}}
 
}}
  
Line 125: Line 141:
 
{{dsc inc|cpp/language/dsc alignof}}
 
{{dsc inc|cpp/language/dsc alignof}}
 
{{dsc inc|cpp/language/dsc sizeof...}}
 
{{dsc inc|cpp/language/dsc sizeof...}}
 +
{{dsc inc|cpp/types/dsc numeric_limits}}
 
{{dsc see c|c/language/sizeof}}
 
{{dsc see c|c/language/sizeof}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 01:28, 14 August 2024

 
 
C++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (C++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++17*)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
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
explicit (C++11)
static

Special member functions
Templates
Miscellaneous
 
 

Queries size of the object or type.

Used when actual size of the object must be known.

Contents

[edit] Syntax

sizeof( type ) (1)
sizeof expression (2)

Both versions are constant expressions of type std::size_t.

type - a type-id (see type naming)
expression - an expression whose operator precedence is not lower than sizeof (e.g. sizeof a + b is parsed as (sizeof a) + b instead of sizeof (a + b))

[edit] Explanation

1) Yields the size in bytes of the object representation of type.
2) Yields the size in bytes of the object representation of the type of expression, if that expression is evaluated.

[edit] Notes

Depending on the computer architecture, a byte may consist of 8 or more bits, the exact number being recorded in CHAR_BIT.

The following sizeof expressions always evaluate to 1:

  • sizeof(char)
  • sizeof(signed char)
  • sizeof(unsigned char)
(since C++17)
  • sizeof(char8_t)
(since C++20)

sizeof cannot be used with function types, incomplete types, or bit-field lvalues(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 a potentially-overlapping subobject may be less than the size of that object.

The result of sizeof is always nonzero, even if applied to an empty class type.

When applied to an expression, sizeof does not 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)

[edit] Keywords

sizeof

[edit] Example

The example output corresponds to a system with 64-bit pointers and 32-bit int (a.k.a. LP64 or LLP64).

#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 ? throw 1 : 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:              1
2) sizeof pointer:                  8
3) sizeof(Bit) class:               4
4) sizeof(int[10]) array of 10 int: 40
5) sizeof a        array of 10 int: 40
6) length of array of 10 int:       10
7) length of array of 10 int (2):   10
8) sizeof the Derived class:        8
9) sizeof the Derived through Base: 4
A) sizeof(unsigned):                4
B) sizeof(int):                     4
C) sizeof(short):                   2
D) sizeof(char):                    1
E) sizeof(CharChar):                2
F) sizeof(CharCharInt):             8
G) sizeof(IntCharChar):             8
H) sizeof(CharIntChar):             12
I) sizeof(CharShortChar):           6
J) sizeof f():                      1
K) sizeof Base::a:                  4

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
CWG 1122 C++98 result type of sizeof (std::size_t) was circularly defined it is defined in the same way as in C
CWG 1553 C++11 sizeof could be used with bit-field xvalues prohibited

[edit] See also

alignof operator(C++11) queries alignment requirements of a type[edit]
sizeof... operator(C++11) queries the number of elements in a parameter pack[edit]
provides an interface to query properties of all fundamental numeric types
(class template) [edit]
C documentation for sizeof