C ++ memory layout (2) virtual inheritance-empty virtual base classs (empty base class)

Source: Internet
Author: User

The last time I provided a code response,

Since C ++ stand (C ++ Standards Committee) does not specify specific implementation methods, each compiler vendor may adopt different implementation methods,

The methods used in different ages may be different, and different results may be obtained. Here, I use vc7.1 as a debugging tool

Deeply analyzes the memory layout in the C ++ virtual inheritance step by step;

This section starts with empty virtual base calss (empty base class: Empty calss) and shows how Virtual inheritance is implemented in vc7.1.

The code for testing is as follows:

1. Diamond inheritance mode

Class
{};

Class B: virtual public
{
};

Class C: virtual public
{
};

Class D: Public C, public B
{
};

Int _ tmain (INT argc, _ tchar * argv [])
{
A;
B;
C;
D;

Int * Pd = (int *) & D;
Int * pd_c = (int *) (C *) (& D );
Int * pd_ B = (int *) (B *) (& D );
Int * pd_a = (int *) (A *) (& D );

Cout <sizeof (a) <Endl;
Cout <sizeof (B) <Endl;
Cout <sizeof (c) <Endl;
Cout <sizeof (d) <Endl;
Cout <PD <Endl;
Cout <pd_c <Endl;
Cout <pd_ B <Endl;
Cout <pd_a <Endl;

Return 0;
}

 

Someone will ask,

Class
{};

A A. What is the size of? Of course, there is nothing in A. Let's just think it is empty. But how can the compiler remember?

Is it null? Can you think of an implementation method? Therefore, in order to remember null A, the compiler adds a char mark inside a as its

The unique identifier so that the compiler can recognize a. If you are interested, you can try to define a A, B. Then you can check whether a and B are

Like? At this stage, it should be different. Maybe someone will invent a new implementation method in the future. At that time, it may be the same.

Now let's take a look at the output of the above program, as shown below:
1
4
4
8
0012 feac
0012 feac
0012feb0
0012feb4

Here I will not explain the above output first. Maybe you may wonder if it is different from what you think? With questions

Continue to look below.
Below I will give two more sections of code, and give their respective output, and then explain the output:

2. hexagonal (diamond 1)
Class
{};

Class B: virtual public
{
};

Class C: virtual public
{
};

Class D:/* virtual */Public B
{
};

Class E:/* virtual */Public C
{

};
Class F: Public E, public d
{
};
Int _ tmain (INT argc, _ tchar * argv [])
{
A;
B;
C;
D;
E;
F;

Int * pF = (int *) & F;
Int * pf_e = (int *) (E *) (& F );
Int * pf_d = (int *) (D *) (& F );
Int * pf_c = (int *) (C *) (& F );
Int * pf_ B = (int *) (B *) (& F );
Int * pf_a = (int *) (A *) (& F );

Cout <sizeof (a) <Endl;
Cout <sizeof (B) <Endl;
Cout <sizeof (c) <Endl;
Cout <sizeof (d) <Endl;
Cout <sizeof (f) <Endl;
Cout <pf <Endl;
Cout <pf_e <Endl;
Cout <pf_d <Endl;
Cout <pf_c <Endl;
Cout <pf_ B <Endl;
Cout <pf_a <Endl;

Return 0;
}
Output result:
1
4
4
4
4
8
0012fe94
0012fe94
0012fe98
0012fe94
0012fe98
0012fe9c

Hexagonal (diamond)

Class
{};

Class B: virtual public
{
};

Class C: virtual public
{
};

Class D: virtual public B
{
};

Class E: virtual public C
{

};
Class F: Public E, public d
{
};
Int _ tmain (INT argc, _ tchar * argv [])
{
A;
B;
C;
D;
E;
F;

Int * pF = (int *) & F;
Int * pf_e = (int *) (E *) (& F );
Int * pf_d = (int *) (D *) (& F );
Int * pf_c = (int *) (C *) (& F );
Int * pf_ B = (int *) (B *) (& F );
Int * pf_a = (int *) (A *) (& F );

Cout <sizeof (a) <Endl;
Cout <sizeof (B) <Endl;
Cout <sizeof (c) <Endl;
Cout <sizeof (d) <Endl;
Cout <sizeof (e) <Endl;
Cout <sizeof (f) <Endl;
Cout <pf <Endl;
Cout <pf_e <Endl;
Cout <pf_d <Endl;
Cout <pf_c <Endl;
Cout <pf_ B <Endl;
Cout <pf_a <Endl;

Return 0;
}
Output result:
1
4
4
8
8
16
0012fe84
0012fe84
0012fe88
0012fe8c
0012fe90
0012fe8c

The first, second, and third are interesting phenomena. You can consider them as follows :)

We can see that there are 4 more bytes in the virtual space. Why?
In the VC compiler, virtual base calss pointer is used to implement virtual inheritance, so as to ensure that the subclass goes back to the base class.

Only one entity exists. The four extra bytes are used to store their respective addresses pointing to the virtual base class.

You may ask, the size of a is 1, B, c ...... The memory size is only increased by four or four. Isn't the memory size bigger?

Have you seen a problem mentioned above?
The compiler identifies a only when it remembers a. if it knows that A is empty, it has a subclass, and when the subclass represents it,

Of course, the base class exists here :)

You may ask again? So why not represent the subclass as 1? Of course not, as mentioned above, because it is used to implement virtual

Only one entity of the base class exists in the subclass of the inherited diamond inheritance style. Therefore, the pointer method is used instead

Put the body in the sub-class memory.

In the future, parameters, functions, and virtual functions will be constantly added to analyze the Memory Distribution in virtual inheritance.

Statement: If a website is reposted, add the source and connection to the webpage. Thank you. Stay up late and stay up late ~~~
PS: too sleepy to open your eyes ~~~~ No, it's time to go to bed ~~~~ Sorry for the mistake.

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.