Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/language/sizeof"

From cppreference.com
< cpp‎ | language
(Added to example to demonstrate that the sizeof a class can depend on the order of its member variables. Also gave a better description of the 2 instances of "size of array of 10 int" (before, their description used "(2)" to distinguish them).)
(Added sizeof(CharChar) = 2 for contrast to sizeof(CharCharInt) = 8 to demonstrate padding.)
Line 54: Line 54:
 
struct CharIntChar { char c; int i; char c2; };
 
struct CharIntChar { char c; int i; char c2; };
 
struct CharCharInt { char c; char c2; int i; };
 
struct CharCharInt { char c; char c2; int i; };
 
+
struct CharChar { char c; char c2; };
 +
 
int main()
 
int main()
 
{
 
{
Line 63: Line 64:
 
     int a[10];
 
     int a[10];
 
     std::cout  
 
     std::cout  
       << "1) sizeof empty class:              " << sizeof e       << '\n'
+
       << "1) sizeof empty class:              " << sizeof e         << '\n'
       << "2) sizeof pointer:                  " << sizeof &e       << '\n'
+
       << "2) sizeof pointer:                  " << sizeof &e       << '\n'
//    << "3) sizeof function:                " << sizeof(void()) << '\n' // error
+
//    << "3) sizeof function:                " << sizeof(void())   << '\n' // error
//    << "4) sizeof incomplete type:          " << sizeof(int[])   << '\n' // error
+
//    << "4) sizeof incomplete type:          " << sizeof(int[])   << '\n' // error
//    << "5) sizeof bit field:                " << sizeof bit.bit << '\n' // error
+
//    << "5) sizeof bit field:                " << sizeof bit.bit   << '\n' // error
       << "6) sizeof(Bit) class:              " << sizeof(Bit)     << '\n'
+
       << "6) sizeof(Bit) class:              " << sizeof(Bit)     << '\n'
       << "7) sizeof(int[10]) array of 10 int: " << sizeof(int[10]) << '\n'
+
       << "7) sizeof(int[10]) array of 10 int: " << sizeof(int[10]) << '\n'
       << "8) sizeof a        array of 10 int: " << sizeof a       << '\n'
+
       << "8) sizeof a        array of 10 int: " << sizeof a         << '\n'
 
       << "9) length of array of 10 int:      " << ((sizeof a) / (sizeof *a))  << '\n'
 
       << "9) length of array of 10 int:      " << ((sizeof a) / (sizeof *a))  << '\n'
 
       << "A) length of array of 10 int (2):  " << ((sizeof a) / (sizeof a[0])) << '\n'
 
       << "A) length of array of 10 int (2):  " << ((sizeof a) / (sizeof a[0])) << '\n'
       << "B) sizeof the Derived class:        " << sizeof d       << '\n'
+
       << "B) sizeof the Derived class:        " << sizeof d         << '\n'
       << "C) sizeof the Derived through Base: " << sizeof b       << '\n'
+
       << "C) sizeof the Derived through Base: " << sizeof b         << '\n'
       << "D) sizeof(unsigned)                " << sizeof(unsigned)<< '\n'
+
       << "D) sizeof(unsigned)                " << sizeof(unsigned) << '\n'
       << "E) sizeof(char)                    " << sizeof(char)   << '\n'
+
       << "E) sizeof(char)                    " << sizeof(char)     << '\n'
       << "F) sizeof(int)                      " << sizeof(int)     << '\n'
+
       << "F) sizeof(int)                      " << sizeof(int)     << '\n'
 
       << "G) sizeof(CharIntChar)              " << sizeof(CharIntChar) << '\n'
 
       << "G) sizeof(CharIntChar)              " << sizeof(CharIntChar) << '\n'
       << "H) sizeof(CharCharInt)              " << sizeof(CharCharInt) << '\n';
+
       << "H) sizeof(CharCharInt)              " << sizeof(CharCharInt) << '\n'
 +
      << "I) sizeof(CharChar)                " << sizeof(CharChar) << '\n';
 
}
 
}
 
  | p=true
 
  | p=true
Line 97: Line 99:
 
G) sizeof(CharIntChar)              12
 
G) sizeof(CharIntChar)              12
 
H) sizeof(CharCharInt)              8
 
H) sizeof(CharCharInt)              8
 +
I) sizeof(CharChar)                2
 
}}
 
}}
  

Revision as of 12:34, 1 April 2022

 
 
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

Syntax

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

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

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.

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 glvalues.

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, 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)

Keywords

sizeof

Example

The example output corresponds to a system with 64-bit pointers and 32-bit int.

#include <iostream>
 
struct Empty {};
struct Base { int a; };
struct Derived : Base { int b; };
struct Bit { unsigned bit: 1; };
struct CharIntChar { char c; int i; char c2; };
struct CharCharInt { char c; char c2; int i; };
struct CharChar { char c; char c2; };
 
int main()
{
    Empty e;
    Derived d;
    Base& b = d;
    [[maybe_unused]] Bit bit;
    int a[10];
    std::cout 
      << "1) sizeof empty class:              " << sizeof e         << '\n'
      << "2) sizeof pointer:                  " << sizeof &e        << '\n'
//    << "3) sizeof function:                 " << sizeof(void())   << '\n' // error
//    << "4) sizeof incomplete type:          " << sizeof(int[])    << '\n' // error
//    << "5) sizeof bit field:                " << sizeof bit.bit   << '\n' // error
      << "6) sizeof(Bit) class:               " << sizeof(Bit)      << '\n'
      << "7) sizeof(int[10]) array of 10 int: " << sizeof(int[10])  << '\n'
      << "8) sizeof a        array of 10 int: " << sizeof a         << '\n'
      << "9) length of array of 10 int:       " << ((sizeof a) / (sizeof *a))   << '\n'
      << "A) length of array of 10 int (2):   " << ((sizeof a) / (sizeof a[0])) << '\n'
      << "B) sizeof the Derived class:        " << sizeof d         << '\n'
      << "C) sizeof the Derived through Base: " << sizeof b         << '\n'
      << "D) sizeof(unsigned)                 " << sizeof(unsigned) << '\n'
      << "E) sizeof(char)                     " << sizeof(char)     << '\n'
      << "F) sizeof(int)                      " << sizeof(int)      << '\n'
      << "G) sizeof(CharIntChar)              " << sizeof(CharIntChar) << '\n'
      << "H) sizeof(CharCharInt)              " << sizeof(CharCharInt) << '\n'
      << "I) sizeof(CharChar)                 " << sizeof(CharChar) << '\n';
}

Possible output:

1) sizeof empty class:              1
2) sizeof pointer:                  8
6) sizeof(Bit) class:               4
7) sizeof(int[10]) array of 10 int: 40
8) sizeof a        array of 10 int: 40
9) length of array of 10 int:       10
A) length of array of 10 int (2):   10
B) sizeof the Derived class:        8
C) sizeof the Derived through Base: 4
D) sizeof(unsigned)                 4
E) sizeof(char)                     1
F) sizeof(int)                      4
G) sizeof(CharIntChar)              12
H) sizeof(CharCharInt)              8
I) sizeof(CharChar)                 2

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]
C documentation for sizeof