C++ sizeof运算符

C++ sizeof运算符 首页 / C++入门教程 / C++ sizeof运算符

sizeof()是一个运算符,用于判断数据类型,常量,变量的大小。它是一个编译时运算符,因为它在编译时返回任何变量或常量的大小。

由sizeof()运算符计算的大小是计算机中占用的RAM数量。

sizeof()运算符的语法如下:

sizeof(data_type);

在以上语法中,data_type可以是数据的数据类型,变量,常量,并集,结构或任何其他用户定义的数据类型。

sizeof()运算符可以应用于以下操作数类型:

  • 操作数据类型

如果 sizeof()运算符的参数包含变量的数据类型,则 sizeof()运算符将返回数据类型的大小。

让我们通过示例了解这种情况。

#include <iostream>
using namespace std;
int main()
{
  //确定每种数据类型占用的字节空间。
  std::cout << "Size of integer data type : " <<sizeof(int)<< std::endl;
  std::cout << "Size of float data type : " <<sizeof(float)<< std::endl;
  std::cout << "Size of double data type : " <<sizeof(double)<< std::endl;
  std::cout << "Size of char data type : " <<sizeof(char)<< std::endl;
  return 0;
}

在上面的程序中,我们通过使用sizeof()运算符评估了内置数据类型的大小。我们知道int占4个字节,float占4个字节,double占8个字节,char占1个字节,并且sizeof()运算符显示了相同的结果,如下面的输出所示。

sizeof() operator in C++
  • 操作Class类型。
#include <iostream>
using namespace std;
class Base
{
  int a;
};
int main()
{
  Base b;
  std::cout << "Size of class base is : "<<sizeof(b) << std::endl;
  return 0;
}

在上面的程序中,我们评估了具有单个整数变量的类的大小。输出将是4个字节,因为int变量占用4个字节。

sizeof() operator in C++

如果我们在一个类中再添加一个整数变量,则代码将如下所示:

#include <iostream>
using namespace std;
class Base
{
    int a;
    int d;
};
int main()
{
  Base b;
  std::cout << "Size of class base is : "<<sizeof(b) << std::endl;
  return 0;
}

在上面的代码中,我们添加了另一个整数变量。在这种情况下,类的大小为8个字节,因为 int 变量占用4个字节,因此两个整数变量占用8个字节。

输出

sizeof() operator in C++

如果在上面的代码中添加一个char变量,则代码将如下所示:

#include <iostream>

using namespace std;

class Base
{
    int a;
    int d;
    char ch;
};
int main()
{
  Base b;
  std::cout << "Size of class base is : "<<sizeof(b) << std::endl;
  return 0;
}

在上面的代码中,该类具有两个整数变量和一个char变量。根据我们的计算,该类的大小将等于9个字节(int + int + char),但是由于结构(structure)填充的概念,这是错误的。

输出

sizeof() operator in C++
  • 操作数组类型。
#include <iostream>
using namespace std;
 int main()
{
  int arr[]={10,20,30,40,50};
  std::cout << "Size of the array 'arr' is : "<<sizeof(arr) << std::endl;
  return 0;
}

在上面的程序中,我们声明了一个整数类型的数组,其中包含五个元素。我们已经使用 sizeof()运算符评估了数组的大小。根据我们的计算,由于int数据类型占用4个字节,并且数组包含5个元素,因此数组的大小应为20个字节,因此此数组占用的总存储空间为5 * 4 = 20字节。我们在以下输出中可以看到, sizeof()运算符也显示了相同的结果。

sizeof() operator in C++

让我们考虑数组的另一种情况。

#include <iostream>
using namespace std;
void fun(int arr[])
{
    std::cout << "Size of array is : " <<sizeof(arr)<< std::endl;
}
int main()
{
  int arr[]={10,20,30,40,50};
  fun(arr);
  return 0;
}

在上面的程序中,我们尝试使用函数打印数组的大小。在这种情况下,我们创建了一个整数类型的数组,并将' arr '传递给函数 fun() fun()将返回整数指针的大小,即 int * ,而int *的大小在64位操作系统中为8个字节。

输出

sizeof() operator in C++
  • 操作指针类型。
#include <iostream>
using namespace std;
int main()
{
    int *ptr1=new int(10);
    std::cout << "size of ptr1 : " <<sizeof(ptr1)<< std::endl;                                                                                                                                                                                                                                
    std::cout << "size of *ptr1 : " <<sizeof(*ptr1)<< std::endl;
    char *ptr2=new char('a');
    std::cout <<"size of ptr2 : " <<sizeof(ptr2)<< std::endl;
    std::cout <<"size of *ptr2 : "<<sizeof(*ptr2)<< std::endl;
    double *ptr3=new double(12.78);
    std::cout <<"size of ptr3 : " <<sizeof(ptr3)<< std::endl;
    std::cout <<"size of *ptr3 : "<<sizeof(*ptr3)<< std::endl;
    return 0;
}

在上面的程序中,我们确定了指针的大小。对于所有数据类型,指针的大小将保持不变。如果计算机具有32位操作系统,则指针的大小将为4个字节。如果计算机具有64位操作系统,则指针的大小将为8个字节。我在64位上运行此程序,因此输出为8个字节。现在,如果我们向指针提供" *"符号,则输出取决于数据类型,例如,* ptr1是整数类型,意味着int数据类型占用4个字节,因此sizeof()运算符将返回4个字节。

输出

sizeof() operator in C++
  • 操作表达式。
#include <iostream>
using namespace std;
 
int main()
{
   int num1;
   double num2;
   cout << sizeof(num1+num2);
   return 0;
}

在上面的程序中,我们声明了两个分别为int和double类型的变量num1和num2。 int的大小为4个字节,而double的大小为8个字节。结果将是变量,它是占用8个字节的双精度类型。

输出

sizeof() operator in C++

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

从0开始学游戏开发 -〔蔡能〕

OpenResty从入门到实战 -〔温铭〕

重学线性代数 -〔朱维刚〕

手把手教你玩音乐 -〔邓柯〕

零基础实战机器学习 -〔黄佳〕

手把手带你写一个Web框架 -〔叶剑峰〕

搞定音频技术 -〔冯建元 〕

郭东白的架构课 -〔郭东白〕

深入拆解消息队列47讲 -〔许文强〕

好记忆不如烂笔头。留下您的足迹吧 :)