sizeof
运算符
来自 cppreference.cn
查询对象或类型的大小。
当必须知道对象的实际大小时使用。
内容 |
[编辑] 语法
sizeof( type ) |
(1) | ||||||||
sizeof expression
|
(2) | ||||||||
2) 产生 expression 的类型(如果该表达式被求值)的对象表示的大小(以字节为单位)。
type | - | 一个 类型标识(参见 类型命名) |
expression | - | 一个表达式,其运算符优先级不低于 sizeof (例如,sizeof a + b 被解析为 (sizeof a) + b 而不是 sizeof (a + b)) |
sizeof
表达式的结果是 std::size_t 类型的常量表达式。
[编辑] 注解
根据计算机架构,一个 字节 可能由 8 个或更多位组成,确切的数字记录在 CHAR_BIT 中。
以下 sizeof
表达式总是求值为 1
- sizeof(char)
- sizeof(signed char)
- sizeof(unsigned char)
|
(自 C++17 起) |
|
(自 C++20 起) |
sizeof
不能用于函数类型、不完整类型或位域 左值(直到 C++11)泛左值(自 C++11 起)。
当应用于引用类型时,结果是被引用类型的大小。
当应用于类类型时,结果是由该类的完整对象占用的字节数,包括将此类对象放置在数组中所需的任何额外填充。由可能重叠的子对象占用的字节数可能小于该对象的大小。
sizeof
的结果始终为非零,即使应用于空类类型也是如此。
当应用于表达式时,sizeof
不求值表达式(即,该表达式是未求值的操作数)(自 C++11 起),即使表达式指定多态对象,结果也是表达式的静态类型的大小。不执行左值到右值、数组到指针或函数到指针的转换。 然而,临时具体化是(正式地)为纯右值参数执行的:如果参数不可析构,则程序是非良构的。(自 C++17 起)
[编辑] 关键字
[编辑] 示例
示例输出对应于具有 64 位指针和 32 位整数(又名 LP64 或 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 }
可能的输出
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
[编辑] 缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
CWG 1553 | C++11 | sizeof 可以与位域 xvalue 一起使用 |
禁止 |
[编辑] 参见
alignof (C++11) |
查询类型的对齐要求 (运算符) |
sizeof... 运算符 (C++11) |
查询 包中元素的数量 |
提供一个接口来查询所有基本数值类型的属性 (类模板) | |
C 文档 关于 sizeof
|