sizeof
.
sizeof( type )
|
(1) | ||||||||
sizeof expression
|
(2) | ||||||||
std::size_t .
Notes
, byte 8 . CHAR_BIT .
sizeof(char), sizeof(signed char) sizeof(unsigned char) 1 ..
sizeof , , bit-field glvalues .
reference type , 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.
class type , 1 .
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)
64-bit pointers 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;
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 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 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
|