Virtual inheritance Memory Model Analysis

Source: Internet
Author: User

 

Here we will explain from the following points:

  • Virtual inheritance and virtual base class
  • Cl command under vs2010
  • Memory Model
Virtual inheritance and virtual base class

Virtual inheritance: contains the inheritance relationship of virtual keywords in the inheritance definition;
Virtual base class: The base class inherited from virtual in the virtual inheritance system. Note that: class CSubClass: public virtual CBase {}; CBase is called the virtual base class of CSubClass, instead, CBase is a virtual base class, because CBase can not be a base class in the virtual inheritance system.

Cl command under vs2010

Microsoft VS2010 provides a new option to show the layout of C ++ objects in the memory. This option is:

[Cpp]View plaincopyprint?
  1. /D1reportSingleClassLayout

The usage is as follows. After writing the corresponding cpp file, you need to start the Command line tool "Visual Studio 2010 Command Prompt" in VS2010. After switching to the directory where the cpp file is located, enter the following Command:

[Cpp]View plaincopyprint?
  1. Cl [filename]. cpp/d1reportSingleClassLayout [className]

Cl is of course the MS compiler; [filename]. cpp is the cpp file where the class you want to view is located (it doesn't matter if the class is defined in the header file, or you just need to compile the cpp file). You need to add [className] at the end, that is, the class name you want to view.

[Example] the code of the test. cpp file is as follows:

[Cpp]View plaincopyprint?
  1. # Include <iostream>
  2. Using namespace std;
  3. Class Base
  4. {
  5. Public:
  6. Int;
  7. Virtual void fcn (){};
  8. };
  9. Class Derived: public Base
  10. {
  11. Public:
  12. Virtual void fcn2 (){};
  13. Private:
  14. Int d;
  15. Void fcn3 (){}
  16. };
  17. Int main ()
  18. {
  19. }

To view the layout of objects in the Derived class in the memory, you can use the following command line:

[Cpp]View plaincopyprint?
  1. Cl Test. cpp/d1reportSingleClassLayoutDerived

The result is as follows:

We can see that the memory layout of the class Derived object contains the Base class Base object at the beginning of the Derived class object. There is a virtual table pointer pointing to the following Derived :: $ vftable @ (virtual function table). The table contains all the virtual functions in the Derived class.

Memory Model

In this section, I will talk about the memory model of virtual inheritance from the questions he has interviewed.

 

[Cpp]View plaincopyprint?
  1. Class
  2. {
  3. Virtual void ()
  4. {
  5. }
  6. };
  7. Class A1
  8. {
  9. Virtual void ()
  10. {
  11. }
  12. };
  13. Class B: public A, virtual public A1
  14. {
  15. };
  16. Void main ()
  17. {
  18. Cout <"sizeof A:" <sizeof (A) <endl;
  19. Cout <"sizeof A1:" <sizeof (A1) <endl;
  20. Cout <"sizeof B:" <sizeof (B) <endl ;//
  21. }

The output result is:

Enter:

[Cpp]View plaincopyprint?
  1. Cl test. cpp/d1reportSingleClassLayoutB

The memory layout shows the size of class A, class A1, and ClassB, the size of class A should be 1 bytes memory positioning size plus the virtual function pointer 4 bytes. Because of the virtual function pointer, The placeholder value of 1 bytes can be canceled. Therefore, the size of A is 4 bytes, and the same is true for Class A1. For Class B, it mainly comes from class A and class A1 (Virtual inheritance, therefore, B contains A vbptr pointer pointing to the virtual base class (A1) because both A and A1 are virtual inheritance. Here I want to make a picture more intuitive:

So the size of class B is 12 bytes.

[Cpp]View plaincopyprint?
  1. Class
  2. {
  3. Int;
  4. };
  5. Class B
  6. {
  7. Int B;
  8. };
  9. Class C
  10. {
  11. };
  12. Class D
  13. {
  14. };
  15. Class E: public virtual A, public virtual B, public virtual C, public virtual D
  16. {
  17. };
  18. Void main ()
  19. {
  20. Cout <"sizeof E:" <sizeof (E) <endl ;//
  21. }

The output result of Code 2 above:

Whether the size of class E is a bit strange. input the following in the command line:

[Cpp]View plaincopyprint?
  1. Cl test. cpp/d1reportSingleClassLayoutE

Like the preceding black font annotation, because classE is multi-virtual inheritance, the layout in the memory is divided into fixed local and shared local, and the fixed local size is int, B is 8 bytes. In my previous blog post (the mysteries of the VC ++ object layout: virtual functions, multi-inheritance, and virtual inheritance), I will give a rough explanation of this memory type.

[Cpp]View plaincopyprint?
  1. E: $ vbtable @:
  2. 1> 0 | 0
  3. 1> 1 | 4 (Ed (E + 0))
  4. 1> 2 | 8 (Ed (E + 0) B)
  5. 1> 3 | 12 (Ed (E + 0) C)
  6. 1> 4 | 16 (Ed (E + 0) D)

4 In This Place indicates the offset between the vbtable of E and the first address of the virtual base class A. Similarly, 8, 12, and 16 do not need to be mentioned. Since the address offset of the virtual base class in the vbtable domain is given, it indicates that the allocated space still exists in the memory of the E object.

From the perspective of vs2010 memory, it is indeed allocated. Based on the preceding vbtable offset, the object size is 16 bytes (a is the first address of class A and B is the first address of class B ). Someone may ask why it is not 20 bytes? The above memory allocation offset is 16, indicating 20 bytes. The offset is 16, and the 4 bytes C is allocated under variable B (this is not accurate, but it is clear). In this case, the offset is D, and vbtable can be located normally now, if Class D is empty, there is no need to allocate 4 bytes space. Therefore, the size of sizeof E should be: vbtable pointer (4 bytes) + fixed local (a, B 8 bytes) + C 4 bytes (this is a bit difficult to understand, but I still have examples below to deepen understanding ).

 

[Cpp]View plaincopyprint?
  1. Class
  2. {
  3. };
  4. Class B
  5. {
  6. Int B;
  7. };
  8. Class C
  9. {
  10. };
  11. Class D
  12. {
  13. };
  14. Class E: public virtual A, public virtual B, public virtual C, public virtual D
  15. {
  16. };
  17. Void main ()
  18. {
  19. Cout <"sizeof A:" <sizeof (A) <endl;
  20. Cout <"sizeof B:" <sizeof (B) <endl;
  21. Cout <"sizeof C:" <sizeof (C) <endl;
  22. Cout <"sizeof D:" <sizeof (D) <endl;
  23. Cout <"sizeof E:" <sizeof (E) <endl ;//
  24. }

The output result of Code 2 above:

This time it seems strange. Let's look at the memory layout.

First, check if vbtable is used. Is it strange that the offset of A and B is 4, and the ClassA itself is empty? Just because the ClassB has A member, you must allocate memory to the classB, so we can find the offset of A. Class C and Class D won't be able to calculate the offset, So we allocated the memory to ClassC, classC has the memory so that the offset of D can be generated. At this time, someone asks why the offset of ClassD is not 8 (I didn't understand it to tell the truth, my guess is that the object itself is empty, but it is 4 bytes for memory alignment. I hope you can give some advice. The following example can better illustrate this point after debugging multiple examples ),

[Cpp]View plaincopyprint?
  1. Class
  2. {
  3. };
  4. Class B
  5. {
  6. };
  7. Class C
  8. {
  9. };
  10. Class D
  11. {
  12. };
  13. Class E: public virtual A, public virtual B, public virtual C, public virtual D
  14. {
  15. };
  16. Void main ()
  17. {
  18. E ee;
  19. Cout <"sizeof A:" <sizeof (A) <endl;
  20. Cout <"sizeof B:" <sizeof (B) <endl;
  21. Cout <"sizeof C:" <sizeof (C) <endl;
  22. Cout <"sizeof D:" <sizeof (D) <endl;
  23. Cout <"sizeof E:" <sizeof (E) <endl ;//
  24. }

The output result is:

Let's look at the memory model.

From the vbtable perspective, the default fixed layout of the Offset class E stored in it is 4 bytes, and the size of classA, class B, and classC in the shared layout is 12 bytes. Therefore, it is 16 bytes.

Source: http://blog.csdn.net/wangqiulin123456/article/details/8059536

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.