sizeof
operator
查询对象或类型的大小。
当需要知道对象的实际大小时使用。
目录 |
语法
sizeof(
类型
)
|
(1) | ||||||||
sizeof
表达式
|
(2) | ||||||||
| 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++代码均未翻译,仅翻译了需要本地化的说明文字。由于原文中除代码外无其他可翻译文本内容,故保持原样输出)
|
(自 C++17 起) |
|
(自 C++20 起) |
sizeof
不能用于函数类型、不完整类型或位域
左值
(C++11 前)
泛左值
(C++11 起)
。
当应用于引用类型时,结果是所引用类型的大小。
当应用于类类型时,结果是该类的完整对象所占用的字节数,包括将此类对象置于数组中所需的任何额外填充。 潜在重叠子对象 所占用的字节数可能小于该对象的大小。
sizeof
的结果始终非零,即使应用于空类类型也是如此。
当应用于表达式时,
sizeof
运算符
不会对表达式进行求值
(即该表达式属于未求值操作数)
(C++11 起)
,即使表达式指向多态对象,其结果仍是表达式静态类型的大小。不会执行左值到右值、数组到指针或函数到指针的转换。
但对于纯右值实参会(正式)执行
临时量实质化
:若实参不可被析构则程序非良构。
(C++17 起)
关键词
示例
示例输出对应具有64位指针和32位int的系统(即 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++标准。
| 缺陷报告 | 应用于 | 发布时的行为 | 正确行为 |
|---|---|---|---|
| CWG 1553 | C++11 |
sizeof
可用于位域右值
|
禁止使用 |
参见
alignof
(C++11)
|
查询类型的对齐要求
(运算符) |
sizeof...
operator
(C++11)
|
查询 参数包 中的元素数量 |
|
提供查询所有基础数值类型属性的接口
(类模板) |
|
|
C 文档
关于
sizeof
|
|