首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

sizeof operator

查询对象或类型的大小。

当必须知道对象的实际大小时使用。

句法

sizeof( type )

(1)

?

sizeof expression

(2)

?

两个版本都返回类型的常量。std::size_t...

解释

1%29返回大小(以字节为单位)对象表示类型的。

2%29返回类型的对象表示的字节大小,如果进行计算,表达式将返回该类型。

注记

取决于计算机架构,字节可由8人组成或更多位,记录在CHAR_BIT...

sizeof(char),,,sizeof(signed char),和sizeof(unsigned char)总是回来1...

不能与函数类型、不完整类型或位字段glvalue一起使用“量值”.

当应用于引用类型时,结果是引用类型的大小。

当应用于类类型时,结果是该类的对象的大小加上将该对象放置在数组中所需的任何附加填充。

应用于空类类型时,始终返回1。

当应用于表达式时,sizeof是吗?不计算表达式,即使表达式指定了多态对象,结果也是表达式的静态类型的大小。不执行Lvalue到rvalue、数组到指针或函数到指针的转换.临时物化但是,对于prvalue参数执行的是%28形式的%29:size决定结果对象的大小。%28自C++17%29。

关键词

sizeof...

示例输出对应于具有64位指针和32位int的系统.

二次

代码语言:javascript
复制
#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';
 
}

二次

可能的产出:

二次

代码语言:javascript
复制
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

二次

另见

  • 对齐
  • 相当大的..。

c大量文件

*。

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com