4

我认为sizeof(Base)应该是 12。为什么是 16?

没有虚函数,我得到 4 和 8。

class Base{
  public:
    int i;
    virtual void Print(){cout<<"Base Print";}
};

class Derived:public Base{
  public:
    int n;
    virtual void Print(){cout<<"Derived Print";}
};

int main(){
  Derived d;
  cout<<sizeof(Base)<<","<<sizeof(d);
  return 0;
}

预期结果:12,16

实际结果:16,16

4

2 回答 2

8

为什么 sizeof(Base) 与 sizeof(Derived) 没有区别

因为编译器引入了对齐。

那是依赖于架构的,但为了简单起见,我假设我们指的是 64 位架构。

场景 64 位/Clang 8.0

类型的对齐方式Base8字节:

alignOfBase(): # @alignOfBase()
  mov eax, 8
  ret

的布局Base由变量成员(int)和虚拟表(vtptr)组成。

如果我们假设一个“通用”架构,其中:

  • int是 4 字节大小。
  • vtptr是一个指针。在 64 位架构上是 8 字节大小。

4 + 8 = 12如您所料,我们应该有 的总和。

但是,我们需要记住 is 的对齐Base方式8 bytes。因此,连续Base类型应存储在 8 的倍数位置。

为了保证这一点,编译器为Base. 这Base就是 16 字节大小的原因。

例如,如果我们考虑 2 个连续的Base(base0base1) 而没有填充:

0:  vtptr (base 0) + 8
8:  int   (base 0) + 4
12: vtptr (base 1) + 8  <--- Wrong! The address 12 is not multiple of 8.
20: int   (base 1) + 4

带填充:

0:  vtptr (base 0) + 8
8:  int   (base 0) + 4+4   (4 padding)
16: vtptr (base 1) +8      <--- Fine! The adress 16 is multiple of 8.
24: int   (base 1) +4+4    (4 padding)

同样的故事也适用于Derived类型。

的布局Derived应该是:vtptr + int + int,即8 + 4 + 4 = 16

的对齐方式Derived也是8

alignOfDerived(): # @alignOfDerived()
  mov eax, 8
  ret

实际上,在这种情况下,不需要引入填充来保持Derived与内存的对齐。布局大小将与实际大小相同。

0:   vtptr (Derived 0)
8:   int   (Derived 0)
12:  int   (Derived 0)
16:  vtptr (Derived 1)  <---- Fine. 16 is multiple of 8.
24:  int   (Derived 1)
28:  int   (Derived 1)
于 2019-03-27T11:49:34.817 回答
1

这是因为编译器决定对齐你的类。

如果您希望(或需要)结构或类具有其“真实”大小,则可以#pragma pack(1)这样使用:

#pragma pack(push, 1) // Set packing to 1 byte and push old packing value to stack

class Base{
  public:
    int i;
    virtual void Print(){cout<<"Base Print";}
};

class Derived:public Base{
  public:
    int n;
    virtual void Print(){cout<<"Derived Print";}
};

int main(){
  Derived d;
  cout<<sizeof(Base)<<","<<sizeof(d);
  return 0;
}

#pragma pack(pop) // restore old packing value
于 2019-03-27T11:27:52.810 回答