Introduction to the memory layout of Objects

Source: Internet
Author: User

Introduction to the memory layout of Objects

Author: sccot ~ 2011-07-29

I. layout of single class objects in memory

In this case, only the member variables of the class occupy space. For example:

class A
{
public:
void funA(){}
public:
int a;
};

If int is represented in four bytes on the machine, it is easy to know that the size of a is 4. However, if I declare the function FUNA as virtual (if the pointer is also expressed in 4 bytes on this platform), the size of a will be 8. If the function is declared as virtual, the class may be a base class. To realize polymorphism, the compiler adds a member variable to the class, is a pointer to the virtual function table (often called vptr), so the size of a is the size of member variable a plus the size of the pointer. The memory distribution is roughly:

If int is represented in four bytes on the machine, it is easy to know that the size of a is 4. However, if I declare the function FUNA as virtual (if the pointer is also expressed in 4 bytes on this platform), the size of a will be 8. If the function is declared as virtual, the class may be a base class. To realize polymorphism, the compiler adds a member variable to the class, is a pointer to the virtual function table (often called vptr), so the size of a is the size of member variable a plus the size of the pointer. The memory distribution is roughly:

Figure 1: Layout of objects of a single class in memory

II. General inheritance in the memory Layout

What is the size of object B? The answer is 12. In this case, whether the function funb is declared as virtual or not, the size of the B object is 12. As shown in figure 1, objects of B include member variables A and B, and pointers to the virtual function table. Because B inherits the pointer from object, therefore, B itself will not add pointer members. The memory distribution of object B is roughly as follows:

Figure 2: Layout inherited in memory

Iii. layout of Multi-inheritance in memory

Now, whether the function funb is declared as virtual or not, the size of the C object is always 20. Including member variables A, B, C and the pointer inherited from A to the virtual function table and the pointer inherited from B to the B virtual function (because a and B both have a pointer member, the two pointer members point to the same position (the virtual function table of C ). The memory allocation of the C object is roughly as follows:

Figure 3: Layout of Multi-inheritance in memory

As shown in figure 3, C has two pointers pointing to the virtual function table of C at the same time, and C has a virtual function funb. When Class B pointers and references are called, C will call B :: funb function. When Class C pointer and Reference call are used, C: funb function is called.

 

Iv. layout of virtual inheritance in memory

For the most difficult part, see the following example:

What is the size of the objects A, B, C, and D?

First case (in GCC): 8, 16, 16, and 28. Now the size of B has changed to 16. As mentioned in the preceding section, it indicates that in GCC, the virtual inheritance itself will add a pointer to the virtual function table, whether or not the base class has a pointer pointing to the virtual function table. In this case, D contains the member variables A, B, C, D, and the three pointers inherited from A, B, and C pointing to the D virtual function table at the same time, therefore, the size of the D object is 28. If you change the declaration of d to class d: virtual public B, and virtual public C, the size of D will change to 32. In this case, the distribution of D in the memory is roughly as follows:

Figure 3: Layout of virtual inheritance in memory (Case 1)

The second case (the case in VC is also what Scott Meyers, author of more than tive C ++, thinks. The second case differs from the first case in that a pointer member is added to the B object and the C object, and the pointer member points to the virtual base class. So now the size of B and C is 20, and D inherits two pointers from class B and class C, so the size of D is 36. In this case, the layout of the D object is roughly as follows:

Figure 4: Layout of virtual inheritance in memory (Case 2)

V. Summary

As shown in the preceding example, declaring a member function of a class as a virtual function and inheriting a declaration as a virtual inheritance result in a great waste of space. In addition, multi-inheritance and virtual inheritance must specify in the derived class which is a member of the base class to use. Therefore, it is not suitable for scenarios with high spatial requirements. This may be two reasons for using interfaces in Java to replace multiple inheritance.

References: more effective C ++ Scott Meyers

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.