C++ sizeof()基础教程文档

收录于 2023-04-20 00:10:05 · بالعربية · English · Español · हिंदीName · 日本語 · Русский язык · 中文繁體

sizeof()是一个运算符,用于评估数据类型,常量,变量的大小。它是一个编译时运算符,因为它在编译时返回任何变量或常量的大小。
由sizeof()运算符计算得出的大小是其中占用的RAM数量。
sizeof()运算符的语法如下:
sizeof(data_type);
在以上语法中,data_type可以是数据,变量,常量,并集,结构或任何其他用户定义的数据类型的数据类型。
sizeof( )运算符可应用于以下操作数类型:
操作数为数据类型
如果 sizeof()运算符的参数包含变量的数据类型,则 sizeof()运算符将返回数据类型。
让我们通过示例了解这种情况。
#include <iostream>
using namespace std;
int main()
{
  // Determining the space in bytes occupied by each data type.
  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()运算符显示了相同的结果,如下面的输出所示。
输出
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()运算符
如果我们在一个类中再添加一个整数变量,则代码将类似于:
#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()在C++中的运算符
如果我们在上面的代码,那么代码将如下所示:
#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),但是由于结构填充的概念,这是错误的。
输出
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()运算符也显示了相同的结果。
输出
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 * ,在64位操作系统中,int *的大小为8个字节。
输出
sizeof()在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()在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个字节。
输出
C++中的运算符