sizeof operator
来自cppreference.com
查詢對象或類型的大小
在需要知道對象的實際大小時使用。
目錄 |
[编辑] 語法
sizeof( type )
|
|||||||||
sizeof expression None
|
|||||||||
兩個版本都返回一個 std::size_t 類型的常量。
[编辑] 解釋
1) 返回 type 類型對應對象的大小(以位元組為單位)。
2) 返回 expression 的返回類型對應對象的大小(以位元組為單位)。
[编辑] 註解
無論 CHAR_BIT 為何值,sizeof(char)、sizeof(signed char)、sizeof(unsigned char) 總是返回 1。
sizeof 不能用於函數類型、不完全類型和位段左值。
當應用於引用類型時,其結果是被引用類型的大小。
當應用於類時,其結果是該類對象的大小與該對象放入數組時所需的填充大小的總和。
當應用於空類時,總是返回 1。
[编辑] 關鍵字
[编辑] 示例
示例輸出表示該系統是 64 位指針、32 位 int 系統
#include <iostream> struct Empty {}; struct Bit {unsigned bit:1; }; int main() { Empty e; Bit b; std::cout << "size of empty class: " << sizeof e << '\n' << "size of pointer : " << sizeof &e << '\n' // << "size of function: " << sizeof(void()) << '\n' // compile error // << "size of incomplete type: " << sizeof(int[]) << '\n' // compile error // << "size of bit field: " << sizeof b.bit << '\n' // compile error << "size of array of 10 int: " << sizeof(int[10]) << '\n'; }
Output:
size of empty class: 1 size of pointer : 8 size of array of 10 int: 40