How to calculate the size of C ++ inheritance, virtual inheritance, and virtual function classes ?, Function

Source: Internet
Author: User

How to calculate the size of C ++ inheritance, virtual inheritance, and virtual function classes ?, Function

How to calculate the size of C ++ inheritance, virtual inheritance, and virtual function classes?

I. Vacuum

C ++ code

ClassCNull

{

};

Length: 1

Memory Structure:

Note: The length is actually 0. This byte is meaningless as the content, and may be different each time.

Ii. Empty category

C ++ code

ClassCNull2

{

Public:

CNull2 () {printf ("Construct/n ");}

~ CNull2 () {printf ("Desctruct/n ");}

VoidFoo () {printf ("Foo/n ");}

};

Length: 1

Memory Structure:

Note: similar to the vacuum class, the internal member functions do not affect the class size.

Iii. Simple

C ++ code

ClassCOneMember

{

Public:

COneMember (intiValue = 0) {m_iOne = iValue ;};

Private:

Intm_iOne;

};

Length: 4

Memory Structure:

00 00 00 00 // m_iOne

Comment: Only member data affects the class size.

Iv. Simple inheritance

C ++ code

ClassCTwoMember: publicCOneMember

{

Private:

Intm_iTwo;

};

Length: 8

Memory Structure:

00 00 00 00 // m_iOne

CC // m_iTwo

Note: subclass members are connected to the parent class members.

5. Re-inherit

C ++ code

ClassCThreemember: publicCTwoMember

{

Public:

CThreemember (intiValue = 10) {m_iThree = iValue ;};

Private:

Intm_iThree;

};

Length: 12

Memory Structure:

00 00 00 00 // m_iOne

CC // m_iTwo

0A 00 00 00 // m_iThree

Comments: The sun class members are inherited after the child class and so on.

Vi. Multi-Inheritance

C ++ code

ClassClassA

{

Public:

ClassA (intiValue = 1) {m_iA = iValue ;};

Private:

Intm_iA;

};

ClassClassB

{

Public:

ClassB (intiValue = 2) {m_iB = iValue ;};

Private:

Intm_iB;

};

ClassClassC

{

Public:

ClassC (intiValue = 3) {m_iC = iValue ;};

Private:

Intm_iC;

};

ClassCComplex: publicClassA, publicClassB, publicClassC

{

Public:

CComplex (intiValue = 4) {m_iComplex = iValue ;};

Private:

Intm_iComplex;

};

Length: 16

Memory Structure:

01 00 00 00 //

02 00 00 00 // B

03 00 00 00 // C

04 00 00 00 // Complex

Comments: it is also a parent class member that appears first. I think this is well understood.

VII. Complex inheritance

I don't want to write any code, so I am afraid that the readers will be dazzled.

Length: 32

Memory Structure:

01 00 00 00 //

02 00 00 00 // B

03 00 00 00 // C

04 00 00 00 // Complex

00 00 00 00 // OneMember

CC // TwoMember

0A 00 00 00 // ThreeMember

05 00 00 00 // VeryComplex

Comment: Put your members at the end.

As long as the "Virtual" is not involved, I think there is no difficulty. Unfortunately, "Virtual" is what we want to study.

8. Let's see "virtual inheritance"

C ++ code

ClassCTwoMember: virtualpublicCOneMember

{

Private:

Intm_iTwo;

};

Length: 12

Memory Structure:

E8 2F 42 00 // pointer, pointing to an array about offsets, and the table pointer of the base class offset

CC // m_iTwo

00 00 00 00 // m_iOne (virtual base data member)

Note: virtual increases the length by 4. In fact, there is an extra pointer. This pointer is indeed complicated. Other articles have specific analysis, so I will not go into details here, it can be considered to point to an array about the virtual base class offset, which is about the offset of the virtual base class data member.

IX. "closed" virtual inheritance to see the effect

Length: 24

Memory Structure:

14 30 42 00 // pointer of the ClassB virtual base class Offset Table

02 00 00 00 // m_iB

C4 2F 42 00 // ClassC virtual base class Offset Table pointer

03 00 00 00 // m_iC

04 00 00 00 // m_iComplex

01 00 00 00 // m_iA

Note: As expected, m_iA, a member of the virtual base class, appears only once and is on the last side. Of course, the more complex situation is much more difficult to analyze than this, but virtual inheritance is not the focus of our research, we only need to know: virtual inheritance uses a "virtual base class Offset Table Pointer" to make the virtual base class appear only once even if it is inherited repeatedly.

10. Check static members.

C ++ code

ClassCStaticNull

{

Public:

CStaticNull () {printf ("Construct/n ");}

~ CStaticNull () {printf ("Desctruct/n ");}

StaticvoidFoo () {printf ("Foo/n ");}

Staticintm_iValue;

};

Length: 1

Memory Structure: (same as CNull2)

Note: it can be seen that static members do not occupy the class size. static members exist in static areas. They can be regarded as "Global" but do not provide global access, this is no different from the static method of C.

11. Empty classes with a virtual function

C ++ code

ClassCVirtualNull

{

Public:

CVirtualNull () {printf ("Construct/n ");}

~ CVirtualNull () {printf ("Desctruct/n ");}

VirtualvoidFoo () {printf ("Foo/n ");}

};

Length: 4

Memory Structure:

00 31 42 00 // pointer to the virtual function table ("virtual table ")

00423100: (virtual table)

41 10 40 00 // pointer to the virtual function Foo

00401041:

E9 78 02 00 00 E9 C3 03... // The content of function Foo (not readable)

Note: The class length with virtual functions is increased by 4, which is actually a pointer to the virtual function table. In the above example, the virtual table has only one function pointer, the value is "0x00401041", and the address pointed to is the function entry.

12. inherit classes with virtual functions

C ++ code

ClassCVirtualDerived: publicCVirtualNull

{

Public:

CVirtualDerived () {m_iVD = 0xFF ;};

~ CVirtualDerived (){};

Private:

Intm_iVD;

};

Length: 8

Memory Structure:

3C 50 42 00 // virtual table pointer

FF 00 00 00 // m_iVD

00w.3c: (virtual table)

23 10 40 00 // pointer to the virtual function Foo. If you create a CVirtualNull object at this time, you will find that the content of its virtual table is the same as this

Note: because the parent class has a virtual function, even if the subclass does not explicitly declare the virtual function, the virtual table still exists. The location of the virtual table is different from that of the parent class, but the content is the same, that is, copying the parent-class virtual table.

13. New virtual functions are available for sub-classes.

C ++ code

ClassCVirtualDerived: publicCVirtualNull

{

Public:

CVirtualDerived () {m_iVD = 0xFF ;};

~ CVirtualDerived (){};

VirtualvoidFoo2 () {printf ("Foo2/n ");};

Private:

Intm_iVD;

};

Length: 8

Memory Structure:

24 61 42 00 // virtual table pointer

FF 00 00 00 // m_iVD

00426124: (virtual table)

23 10 40 00

50 10 40 00

Note: There is only one virtual table. The new virtual function pointer will be added to the copied virtual table instead of the new virtual function.

14. When a pure virtual function (pure function) appears

C ++ code

ClassCPureVirtual

{

VirtualvoidFoo () = 0;

};

ClassCDerivePV: publicCPureVirtual

{

VoidFoo () {printf ("vd: Foo/n ");};

};

Length: 4 (CPureVirtual), 4 (CDerivePV)

Memory Structure:

CPureVirtual:

(Not instantiated)

CDerivePV:

28 50 42 00 // virtual table pointer

00425028: (virtual table)

5A 10 40 00 // function pointer to Foo

Note: classes with pure virtual functions cannot be instantiated, so they cannot be listed as "memory structure", and Their Derived classes implement pure virtual functions. We can see that although CDerivePV does not have a virtual declaration, its parent class still inherits the virtual table because it has a virtual class. If CDerivePV has a subclass, this still applies.

15. Multi-inheritance of virtual function classes

As mentioned above: (Virtual tables of sub-classes) There will be no additional virtual functions, but this will not happen if there are multiple inheritance. In the following example, you will see two virtual tables.

Size: 24

Memory Structure

F8 50 42 00 // virtual table pointer

01 00 00 00 // m_iA

02 00 00 00 // m_iB

E8 50 42 00 // virtual table pointer

03 00 00 00 // m_iC

04 00 00 00 // m_iComplex

0020.f8 :( virtual table)

5A 10 40 00 // FooA

55 10 40 00 // FooB

64 10 40 00 // FooComplex

00w.e8: (virtual table)

5F 10 40 00 // FooC

Note: The virtual function of the subclass is followed by the virtual function table of the first base class, so B is followed by A and Complex is followed by B. The base class appears in sequence, and the Child class members are connected to the end, so m_iComplex is located at the end.

16. Virtual inheritance containing virtual function classes

C ++ code

ClassVirtualInheritance

{

Chark [3];

Public:

Virtualvoidaa (){};

};

ClasssonClass1: publicvirtualVirtualInheritance

{

Charj [3];

Public:

Virtualvoidbb (){};

};

ClasssonClass2: publicvirtualsonClass1

{

Charf [3];

Public:

Virtualvoidcc (){};

};

Intmain ()

{

Cout

Return0;

}

Output result:

Visio studio: 8, 20, 32

Gcc: 8, 16, 24

Comments:

The VirtualInheritance class is 8 in size and has no objection. It has a virtual table pointer vtp_VirtualInheritanc;

For the sonClass1 class:

In the visio studio compiler, the size is 20. Because it is a virtual inheritance and has its own virtual function, it first has its own virtual function pointer vpt_sonClass1 with a size of 4 pointing to its own virtual table; there is also a char [3] with a size of 4. To implement virtual inheritance, sonClass1 first adds a virtual class pointer pointing to its parent class as vtp_sonClass1_VirtualInheritanc with a size of 4; then, all the size of the parent class is 8, so the total size is 20 bytes.

The gcc compiler has a size of 16 and does not calculate the virtual class pointer vtp_sonClass1_VirtualInheritanc pointing to the parent class in the subclass.

For sonClass2:

In the visio studio environment, the size is 32. Similar to the above, the subclass has char [3] and the size is 4 bytes. Because it is a virtual inheritance and has its own virtual function, it has its own virtual table pointer, vtp_sonClass2, the size is 4 bytes. Then there is a virtual class pointer to the parent class vtp_sonClass2_sonClass

1. The size is 4. The total size of the parent class is 20, so the total size is 4 + 4 + 4 + 20 = 32;

In the gcc environment, the size of the virtual class pointer is not calculated, that is, 4 + 4 + 16 = 24.

17. Multiple common and virtual hybrid inheritance containing virtual functions

C ++ code

ClassVirtualInheritance

{

Chark [3];

Public:

Virtualvoidaa (){};

};

ClasssonClass1: publicvirtualVirtualInheritance

{

Charj [3];

Public:

Virtualvoidbb (){};

};

ClassVirtualInheritance2

{

Charl [3];

Public:

Virtualvoiddd (){};

};

ClasssonClass2: publicvirtualsonClass1, publicVirtualInheritance2

{

Charf [3];

Public:

Virtualvoidcc (){};

};

Intmain ()

{

Cout

Return0;

}

Note: The size of sonClass2 is changed to 36. Different from 16, sonClass2 has multiple inheritance, one of which is virtual inheritance and the other is normal inheritance. The size of sonClass2 is changed to 36 in visio studio, which is 4 more than 16, this happens to be the size of char l [3]. Because Song class2 already has a virtual table, you can add one more record to its original virtual table.

18. Multiple virtual inheritance containing virtual functions

C ++ code

ClassVirtualInheritance

{

Chark [3];

Public:

Virtualvoidaa (){};

};

ClasssonClass1: publicvirtualVirtualInheritance

{

Charj [3];

Public:

Virtualvoidbb (){};

};

ClassVirtualInheritance2

{

Charl [3];

Public:

Virtualvoiddd (){};

};

ClasssonClass2: publicvirtualsonClass1, publicvirtualinheritance2

{

Charf [3];

Public:

Virtualvoidcc (){};

};

Intmain ()

{

Cout

Return0;

}

Note: The size of sonClass2 changes to 40. Different from 17, multiple inheritance of sonClass2 is virtual inheritance. The size of sonClass2 consists of the following parts:

1. Its own size. char [3] has a size of 4 and a virtual function. Therefore, there is a pointer to the virtual table. The size is 4, so its total size is 8;

2. The virtual inheritance is sonClass1. Because of the virtual inheritance, there is a virtual class pointer ptr_sonClass2_sonClass1 with the size of 4 and sonClass1 with the size of 20. Therefore, the virtual inheritance is sonClass1 with the size of 24;

3. virtual inheritance VirtualInheritance2, a virtual class pointer cursor = ptr_sonClass2_sonClass1 + offset, the pointer and ptr_sonClass2_sonClass1 share a pointer, but the offset is different, so the size is 0 (even if several more virtual classes are inherited, the pointer size is counted only once), while VirtualInheritance2 is 8, so the total size is 8.

So 40 = 8 + 24 + 8

Summary:

1. For normal single inheritance, you only need to add the size of its member variables to the size of the parent class (there are virtual functions in the parent class, no matter whether there is any in the subclass). If the parent class does not have virtual functions, the subclass size must be indicated by the pointer to the virtual table.

2. Common multi-inheritance. If multiple parent classes have virtual tables, the subclass shares a virtual table pointer with the first parent class, other parent classes with several virtual functions have several virtual table pointers.

3. Virtual single inheritance. If the sub-class has a virtual function, the size of its own virtual table pointer is added (if not, it is not added), and the size of its own member variable is added, add a virtual class pointer ptr_sonclass_fatherclass and the size of the parent class.

4. Multi-virtual inheritance. If the sub-class has a virtual function, the size of its own virtual table pointer will be added (if not, it will not be called), and the size of its own member variable will be added, add a public virtual class pointer (no matter how many virtual parent classes there are, add only one), and add the size of all parent classes.

5. The common and virtual classes inherit from each other. In this case, the subclass size is its own size (if the subclass or common parent class has a virtual function, it is the member variable + virtual table pointer size; if there is no virtual function, it will be the member variable size), add a virtual class pointer size, add the virtual parent class size, add the size of the common parent class (except the virtual table pointer, because it shares a virtual table pointer with the subclass ).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.