Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/language/sizeof"

From cppreference.com
< cpp‎ | language
(Add mention of std::byte size)
m (~ fmt + sizeof(Bit) case)
Line 55: Line 55:
 
     [[maybe_unused]] Bit bit;
 
     [[maybe_unused]] Bit bit;
 
     int a[10];
 
     int a[10];
     std::cout << "size of empty class:              " << sizeof e        << "\n"
+
     std::cout  
              << "size of pointer:                  " << sizeof &e      << "\n"
+
      << "size of empty class:              " << sizeof e        << '\n'
//           << "size of function:               " << sizeof(void())  << "\n" // error
+
      << "size of pointer:                  " << sizeof &e      << '\n'
//           << "size of incomplete type:         " << sizeof(int[])  << "\n" // error
+
//   << "size of function:                 " << sizeof(void())  << '\n' // error
//           << "size of bit field:               " << sizeof bit.bit  << "\n" // error
+
//   << "size of incomplete type:         " << sizeof(int[])  << '\n' // error
              << "size of array of 10 int:         " << sizeof(int[10]) << "\n"
+
//   << "size of bit field:               " << sizeof bit.bit  << '\n' // error
              << "size of array of 10 int (2):     " << sizeof a        << "\n"
+
      << "size of Bit class:                " << sizeof(Bit)    << '\n'
              << "length of array of 10 int:       " << ((sizeof a) / (sizeof *a)) << "\n"
+
      << "size of array of 10 int:         " << sizeof(int[10]) << '\n'
              << "length of array of 10 int (2):   " << ((sizeof a) / (sizeof a[0])) << "\n"
+
      << "size of array of 10 int (2):     " << sizeof a        << '\n'
              << "size of the Derived:              " << sizeof d        << "\n"
+
      << "length of array of 10 int:       " << ((sizeof a) / (sizeof *a)) << '\n'
              << "size of the Derived through Base: " << sizeof b        << "\n";
+
      << "length of array of 10 int (2):   " << ((sizeof a) / (sizeof a[0])) << '\n'
 
+
      << "size of the Derived:              " << sizeof d        << '\n'
 +
      << "size of the Derived through Base: " << sizeof b        << '\n';
 
}
 
}
 
  | p=true
 
  | p=true
Line 72: Line 73:
 
size of empty class:              1
 
size of empty class:              1
 
size of pointer:                  8
 
size of pointer:                  8
size of array of 10 int:         40
+
size of Bit class:                4
size of array of 10 int (2):     40
+
size of array of 10 int:         40
length of array of 10 int:       10
+
size of array of 10 int (2):     40
length of array of 10 int (2):   10
+
length of array of 10 int:       10
 +
length of array of 10 int (2):   10
 
size of the Derived:              8
 
size of the Derived:              8
 
size of the Derived through Base: 4
 
size of the Derived through Base: 4
Line 87: Line 89:
 
{{dsc end}}
 
{{dsc end}}
  
[[de:cpp/language/sizeof]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[es:cpp/language/sizeof]]
+
[[fr:cpp/language/sizeof]]
+
[[it:cpp/language/sizeof]]
+
[[ja:cpp/language/sizeof]]
+
[[pt:cpp/language/sizeof]]
+
[[ru:cpp/language/sizeof]]
+
[[zh:cpp/language/sizeof]]
+

Revision as of 11:52, 21 September 2020

 
 
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.

sizeof(char), sizeof(char8_t), sizeof(signed char),sizeof(unsigned char), and sizeof(std::byte) are always equal to 1.

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 size of an object of that class plus any additional padding required to place such object in an array.

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: sizeof determines the size of the result object.(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; };
 
int main()
{
    Empty e;
    Derived d;
    Base& b = d;
    [[maybe_unused]] Bit bit;
    int a[10];
    std::cout 
      << "size of empty class:              " << sizeof e        << '\n'
      << "size of pointer:                  " << sizeof &e       << '\n'
//    << "size of function:                 " << sizeof(void())  << '\n' // error
//    << "size of incomplete type:          " << sizeof(int[])   << '\n' // error
//    << "size of bit field:                " << sizeof bit.bit  << '\n' // error
      << "size of Bit class:                " << sizeof(Bit)     << '\n'
      << "size of array of 10 int:          " << sizeof(int[10]) << '\n'
      << "size of array of 10 int (2):      " << sizeof a        << '\n'
      << "length of array of 10 int:        " << ((sizeof a) / (sizeof *a)) << '\n'
      << "length of array of 10 int (2):    " << ((sizeof a) / (sizeof a[0])) << '\n'
      << "size of the Derived:              " << sizeof d        << '\n'
      << "size of the Derived through Base: " << sizeof b        << '\n';
}

Possible output:

size of empty class:              1
size of pointer:                  8
size of Bit class:                4
size of array of 10 int:          40
size of array of 10 int (2):      40
length of array of 10 int:        10
length of array of 10 int (2):    10
size of the Derived:              8
size of the Derived through Base: 4

See also

C documentation for sizeof