sizeof类与int,函数,虚函数在C ++? [英] sizeof class with int , function, virtual function in C++?

查看:156
本文介绍了sizeof类与int,函数,虚函数在C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个在线C ++测试问题,已经完成。

This is an online C++ test question, which has been done.

#include<iostream>
using namespace std; 
class A
{

};
class B
{
int i; 
}; 

class C
{
void foo();
};
class D
{
virtual void foo();
};

class E
{
int i ; 
    virtual void foo();
};
class F
{
int i; 
    void foo();
};
class G
{
    void foo();
    int i;
    void foo1();
};

class H
{
    int i ;
    virtual void foo();
    virtual void foo1();
};
int main()
{
cout <<"sizeof(class A) : " << sizeof(A) << endl ;
cout <<"sizeof(class B) adding the member int i : " << sizeof(B) << endl ;
cout <<"sizeof(class C) adding the member void foo() : " << sizeof(C) << endl ;
cout <<"sizeof(class D) after making foo virtual : " << sizeof(D) << endl ;
cout <<"sizeof(class E) after adding foo virtual , int : " << sizeof(E) << endl ;
cout <<"sizeof(class F) after adding foo  , int : " << sizeof(F) << endl ;
cout <<"sizeof(class G) after adding foo  , int : " << sizeof(G) << endl ;
G g;
cout <<"sizeof(class G) after adding foo  , int : " << sizeof(g) << endl ;
cout <<"sizeof(class H) after adding int 2 virtual " << sizeof(H) << endl ;
return 0; 
}

输出:

sizeof(class A) : 1
sizeof(class B) adding the member int i : 4
sizeof(class C) adding the member void foo() : 1
sizeof(class D) after making foo virtual : 8
sizeof(class E) after adding foo virtual , int : 16
sizeof(class F) after adding foo  , int : 4
sizeof(class G) after adding foo   , unsigned int : 4
sizeof(class g) after adding foo  , unsigned int : 4
sizeof(class H) after adding int 2 virtual 16

我的问题:

为什么 siszeof(A)是1, sizeof(C)也是1?

Why siszeof(A) is 1 and sizeof(C) is 1 too ?

为什么 siszeof(H)是16,但 sizeof(G)是4?

Why siszeof(H) is 16 but sizeof(G) is 4 ?

为什么 siszeof(E)是16而 sizeof(F)是4?

Why siszeof(E) is 16 but sizeof(F) is 4 ?

为什么 siszeof(D)是8,但 sizeof(E)是16?

Why siszeof(D) is 8 but sizeof(E) is 16 ?

我的猜测:

虚函数是一个8字节的指针。
但是,我不知道为什么 E size是16?
将函数添加到空类不会更改其大小?

A virtual function is a pointer with 8 bytes. But, I do not know why E size is 16 ? Adding a function to an empty class does not change its size ?

任何帮助。

感谢

推荐答案

首先,虚函数不是指针有8个字节。在C ++中只有 sizeof(char)被保证为任意数量的字节。

First off, a virtual function is not a pointer with 8 bytes. In C++ nothing but sizeof(char) is guaranteed to be any number of bytes.

类中的虚函数增加它的大小(编译器依赖,但大多数 - 如果不是所有 - 它是这样的)。所有后续方法不会。非虚函数不会影响类的大小。

Second, only the first virtual function in a class increases its size (compiler-dependent, but on most - if not all - it's like this). All subsequent methods do not. Non-virtual functions do not affect the class's size.

这是因为类实例不保存指向方法本身的指针,而是指向一个虚函数表,每个类别一个。

This happens because a class instance doesn't hold pointers to methods themselves, but to a virtual function table, which is one per class.

所以,如果你有:

class A
{
   virtual void foo();
}

class B
{
   virtual void goo();
   virtual void test();
   static void m();
   void x();
}

你会有 sizeof(A)== sizeof (B)

现在:


为什么siszeof(A)是1和sizeof(C)也是1?

Why siszeof(A) is 1 and sizeof(C) is 1 too ?

A C 有大小1,因为它不允许一个类的大小为0.函数与它无关。

A and C have size 1 just because it's not allowed for a class to be of size 0. The functions have nothing to do with it. It's just a dummy byte.


为什么siszeof(H)是16而sizeof(G)是4?

Why siszeof(H) is 16 but sizeof(G) is 4 ?

G 只有一个用于记忆的成员 - int 。在你的平台上, sizeof(int)== 4 H 除了 int ,还有一个指向 vftable (虚函数表,见上文)。

G has only one member that accounts for memory - the int. And on your platform, sizeof(int) == 4. H, besides the int, also has a pointer to the vftable (virtual function table, see above). The size of this, size of int and allignment are compiler specific.


为什么siszeof(E)是16,而sizeof(F)是4 ?

Why siszeof(E) is 16 but sizeof(F) is 4 ?

以上解释 - 非虚拟方法不占用类中的内存。

Explained above - non virtual methods don't take up memory in the class.


为什么siszeof(D)是8,但sizeof(E)是16?

Why siszeof(D) is 8 but sizeof(E) is 16 ?

D 只包含 vftable 指针,在您的平台上显然是8个字节。 E 也有一个int, vftable 与8个字节对齐。所以它是这样的:

D only contains the vftable pointer which is apparently 8 bytes on your platform. E also has an int, and the vftable is aligned to 8 bytes. So it's something like:

class E

4 bytes for int |  4 padding bytes  |  8 bytes for vftable pointer  | 
| x | x | x | x |    |    |    |    | v | v | v | v | v | v | v | v |

这篇关于sizeof类与int,函数,虚函数在C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆