C++ 类的大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17281932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:04:20  来源:igfitidea点击:

Size of C++ classes

c++classobjectsizeof

提问by nurabha

Here is the code which prints size of different classes

这是打印不同类大小的代码

#include <iostream>

using namespace std;

class EmptyClass
{    
};

class AbstractClass
{
  public: 
          virtual void funcOne() = 0;
          virtual void funcTwo() = 0;
};

class NotAbstrClass
{
  public: int virtFunc( int );
};

class MixClass
{
  public:
          virtual void clFunc( int );
          static int i;
          int j;
};

int main()
{
    // Print size of class or class objects
    cout<<"Size of empty class: "<< sizeof(EmptyClass)<<endl;          
    cout<<"Size of Abstract class :"<< sizeof(AbstractClass)<<endl;
    cout<<"Size of Non Abstract class: "<< sizeof(NotAbstrClass)<<endl;
    cout<<"Size of Mix class: "<< sizeof(MixClass)<<endl;
    return 0;
}

The output of the program on C++11 compiler is

在 C++11 编译器上程序的输出是

Size of empty class: 1
Size of Abstract class :4
Size of Non Abstract class: 1
Size of Mix class: 8

I understand why Empty class has size 1 Size of empty class object. For abstract class, the object stores a pointer for implementing virtual function call mechanisms. But what about the sizes of other class objects (NotAbstrClass and MixClass) ?

我明白为什么 Empty class 有 size 1 Size of empty class object。对于抽象类,对象存储了一个指针,用于实现虚函数调用机制。但是其他类对象(NotAbstrClass 和 MixClass)的大小呢?

回答by HappyTran

According to Girish Shetty:

根据 Girish Shetty 的说法:

There are many factors that decide the size of an object of a class in C++.

These factors are:

  • Size of all non-static data members
  • Order of data members
  • Byte alignment or byte padding
  • Size of its immediate base class
  • The existence of virtual function(s) (Dynamic polymorphism using virtual functions).
  • Compiler being used
  • Mode of inheritance (virtual inheritance)

在 C++ 中,决定类的对象大小的因素有很多。

这些因素是:

  • 所有非静态数据成员的大小
  • 数据成员的顺序
  • 字节对齐或字节填充
  • 其直接基类的大小
  • 虚函数的存在(使用虚函数的动态多态)。
  • 正在使用的编译器
  • 继承方式(虚拟继承)

Here there are some related website, I think it can be helpful to you.

这里有一些相关的网站,我想它可以对你有所帮助。

Determine the size of class object: http://www.cprogramming.com/tutorial/size_of_class_object.html

确定类对象的大小:http: //www.cprogramming.com/tutorial/size_of_class_object.html

Memory layout: http://www.phpcompiler.org/articles/virtualinheritance.html

内存布局:http: //www.phpcompiler.org/articles/virtualinheritance.html

And, if you use MVSC, you can dump all memory layout of all class in your solution with -d1reportAllClassLayoutlike that:

而且,如果您使用 MVSC,您可以-d1reportAllClassLayout像这样转储解决方案中所有类的所有内存布局:

cl -d1reportAllClassLayout main.cpp

回答by Praetorian

NotAbstrClasshas no data members, so it too is an empty class. Since classes cannot be zero-sized, you get the same treatment as EmptyClass.

NotAbstrClass没有数据成员,所以它也是一个空类。由于类不能为零大小,因此您将获得与EmptyClass.

MixClasshas a virtual function, and 1 non-static data member. It seems each of these (vptrand int) occupy 4 bytes on your platform, so the size is 8 bytes.

MixClass有一个虚函数和 1 个非静态数据成员。似乎这些 (vptrint) 中的每一个都在您的平台上占用 4 个字节,因此大小为 8 个字节。

回答by Marius

NotAbstrClass is like an empty class when we talk about bit sizes since it has no data. MixClass has the virtual function pointer (4 bytes on a 32-bit machine) and an int (also 4 bytes).

当我们谈论位大小时,NotAbstrClass 就像一个空类,因为它没有数据。MixClass 具有虚拟函数指针(在 32 位机器上为 4 个字节)和一个 int(也是 4 个字节)。