名稱空間
變體
操作

sizeof 運算子

來自 cppreference.com
< cpp‎ | 語言
 
 
C++ 語言
 
 

查詢物件或型別的大小。

在需要知道物件實際大小時使用。

目錄

[編輯] 語法

sizeof( 型別 ) (1)
sizeof 表示式 (2)
1) 返回型別物件表示的位元組大小。
2) 返回表示式型別的物件表示的位元組大小,如果該表示式被求值。
型別 - 一個type-id(參見型別命名
表示式 - 一個表示式,其運算子優先順序不低於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 起)
  • sizeof(char8_t)
(C++20 起)

sizeof不能用於函式型別、不完整型別或位域左值(C++11前)glvalue(C++11起)

當應用於引用型別時,結果是引用型別的大小。

當應用於類型別時,結果是該類的完整物件所佔用的位元組數,包括將此類物件放置在陣列中所需的任何額外填充。一個可能重疊的子物件所佔用的位元組數可能小於該物件的大小。

sizeof的結果總是非零,即使應用於空類型別。

當應用於表示式時,sizeof不評估表示式(即表示式是一個未評估的運算元)(C++11起),即使表示式指定一個多型物件,結果也是表示式的靜態型別的大小。不執行左值到右值、陣列到指標或函式到指標的轉換。然而,對於prvalue引數會(形式上)執行臨時物化:如果引數不可銷燬,則程式格式錯誤。(C++17起)

[編輯] 關鍵詞

sizeof

[編輯] 示例

示例輸出對應於一個具有64位指標和32位int的系統(又稱LP64LLP64)。

#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可用於位域xvalues 已禁止

[編輯] 另請參見

alignof (C++11) 查詢型別的對齊要求
(運算子)[編輯]
sizeof... 運算子 (C++11) 查詢中的元素數量[編輯]
提供查詢所有基本數值型別屬性的介面
(類模板) [編輯]
C 文件 瞭解 sizeof