On memory layout of derived class objects in C + + _c language

Source: Internet
Author: User
Tags inheritance

Mainly from three aspects:

1 single inheritance

2 Multiple Inheritance

3 Virtual Inheritance

1 single inheritance

(1) A derived class fully owns the memory layout of the base class and guarantees its integrity.

A derived class can be thought of as the complete base class object plus the derived class own object. If there is no virtual member function in the base class, then the derived class will have no performance difference from the non derived class with the same functionality. Also, be sure to ensure the integrity of the base class. The actual memory layout is determined by the compiler itself, VS, the virtual pointer at the front, then the base class object, and finally the derived class own object. Give me a chestnut:

Class A
{
  int b;
  char c;
};
Class A1:p ublic a
{
    char A;
};
int main ()
{
  cout << sizeof (A) << "" << sizeof (A1) << Endl;
  return 0;
}

What is the output?

Answer:

8 12

A class of words, an int, a char,5b, memory alignment, 8B. A1, one int, two char, memory aligned, and 8 B. Isn't that right?

I said, to ensure the integrity of the base class object. Then be sure to ensure that several bytes in front of the A1 class are exactly the same as Class A. That is to say, the 3 bytes of Class A that are padded as memory are also to appear in the A1. In other words, Class A is like this: Int (4B) +char (1B) +padding (3B) =8B,A1 class: Int (4B) +char (1B) +padding (3B) +char (1B) +padding (3B) =12b.

(2) How does the virtual pointer handle?

or depending on the compiler, VS is always putting vptr at the front of the object. If the base class contains virtual functions, the processing is the same as above. However, if a derived class does not have a virtual function in the base class, then placing the vptr at the front of the derived class will cause the base class component in the derived class to not be in the front. What problems will this bring? Hold Li: Suppose a does not contain imaginary, but A1 contains.

A *pa;
A1 obj_a1;
pa=&obj_a1;

If the A1 fully contains a and a is at the front of the A1, then the compiler simply assigns the &OBJ_A1 directly to the PA. What if it isn't? The compiler needs to assign &obj_a1+sizeof (VPTR) to Pa.

2 Multiple Inheritance

Conclusion: vs is a memory layout that arranges memory in a declarative order. One more chestnut:

Class point2d
{public
:
  virtual ~point2d () {};
  float x;
  float y;
};
Class Point3D:p ublic point2d
{
  ~point3d () {};
  float z;
};
Class vertex
{public
:
  virtual ~vertex () {};
  Vertex* Next;
Class Vertex3d:p ublic Point3D, public vertex
{
  float bulabula;


int _tmain (int argc, _tchar* argv[])
{
  cout << sizeof (POINT2D) << "" << sizeof (Point3D) <& Lt "<< sizeof (vertex) <<" "<< sizeof (VERTEX3D) << Endl;
  return 0;
}

Output: 12 16 8 24.

Memory layout:

Point2d:vptr (4) +x (4) +y (4) =12b

point3d:vptr+x+y+z=16b

vertex:vptr+next=8b

vertex3d:vptr+x+y+z+vptr+next+bulabula=28b

Why do I need multiple virtual pointers? Please look down.

3 Virtual Inheritance

(1) Why should there be a mechanism such as "virtual inheritance"?

In simple terms, virtual inheritance is a problem that also prevents "diamond" inheritance. That is, class A1, A2 are inherited from A, Class B also inherits from A1, A2. As a result, there are two members of Class A in class B, and such programs cannot be compiled. We change to this form:

Class A
{public
:
int A;
Virtual ~a (); 
  virtual void fun () {cout<< "A" <<ENDL;}
;
Class A1:p ublic virtual A
{public
:
int A1;
  virtual void fun () {cout<< "A1" <<ENDL;}
;
Class A2:p ublic virtual A
{public
:
int A2;
  virtual void fun () {cout<< "A2" <<ENDL;}
; 

Class B:p ublic a1,public A2 {public
:
int b;
  virtual void fun () {cout<< "B" <<ENDL;}
  virtual void Funb () {};
};

This will prevent such a thing from happening.

(2) The difference between virtual inheritance and ordinary inheritance:

Normal inheritance enables a derived class to have a member of a base class each inheriting a base class. And the virtual inheritance will put the part of the virtual inheritance, at the end of the object. So that you have only one member of the base class. The offset of the virtual object is saved to the previous slot that is pointed to by the vtbl of the derived class. More difficult to understand. I'll give you a chestnut below.

(3) Virtual inheritance of the memory layout:

Each derived class places its invariant part in front and the shared part behind it.

What is the size of the top four classes?

int _tmain (int argc, _tchar* argv[])
{
  cout << sizeof (A) << "" << sizeof (A1) << "" <& Lt sizeof (A2) << "" << sizeof (B) << Endl;
  return 0;
}

Output: 8 16 16 28

Memory layout:

a:vptr+a=8b

a1:vptr+a1+vptra+a=16b

a2:vptr+a2+vptra+a=16b

a3:vptr+a1+vptra2+a2+b+vptra+a=28b

Last Sketch:

So why do you need multiple virtual pointers? After figuring out the object memory layout and the virtual table structure, is the answer not obvious?

Yes, because it guarantees that when you convert a subclass pointer/reference to a base class pointer, the compiler can offset it directly from the memory layout of the image so that the first content that you point to is a virtual pointer, which in turn enables polymorphism (the corresponding action is performed according to the static type).

The above is a small series for everyone to talk about the memory layout of the derived class object in C + + all content, I hope that we support cloud-Habitat Community ~

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.