Virtual destructor of C + + Object memory model (1)

Source: Internet
Author: User

Read two articles about the C + + object memory model, from here:

In this article, the actual situation of virtual function in many kinds of inheritance patterns is discussed, and the single inheritance is understood as my understanding, and the other kinds are just a variant. Of course, without this article, I can't imagine C + + object memory is like this.

The situation in the article is not about the existence of virtual destructor. I learned the method described in the article, in a heuristic way to find a virtual destructor of the object's memory structure. Virtual destructors are not the same as general virtual functions. I am only preliminary discussion, I hope someone can point, pointing points can also be done.

(The test environment is 32-bit window7,ide is devc++)

Here are a few things to summarize:

(1) A single class, with virtual destructors, virtual functions

(2) Inheritance relationship, single inheritance, no virtual destructor for parent class

(3) Inheritance relationship, single inheritance, parent class with virtual destructor

(4) Is there 4? Don't know for a moment, see summary situation

To be continued, (because UML is learning to use, but also want to write the article beautiful point, not always look at the code, see the picture will understand better.) There's a picture of the truth, that's what it means.


(1) A single class, with virtual destructors, virtual functions

The code is as follows:

1#include <iostream>2 using namespacestd;3 4 classA5 {6        intia;7         Public:8A (): IA (1)9      {        Ten       }  One      Virtual~A () A      { -cout <<"~a"<<Endl; -      }  the      Virtual voidf () -      { -cout <<"a:f ()"<<Endl; -      }  + }; -  +typedefvoid(*F) (); A  at intMain () - { -  -F PF =NULL; -A *a =NewA (); -     int**p = (int**) A; inPF = (F) p[0][2]; - pf (); to  +     intFlag =2; -     Switch(flag) the     { *          Case 0: $PF = (F) p[0][0];Panax Notoginseng pf (); -              Break; the          Case 1: +PF = (F) p[0][1]; A pf (); the              Break; +          Case 2: -PF = (F) p[0][1]; $ pf (); $PF = (F) p[0][0]; - pf (); -              Break; the     } -cout << p[1] <<Endl; Wuyicout << p[0][3] <<Endl; thecout <<sizeof(A) <<Endl; -}

0 a:f ()~A108--------------------Split line---------------------  1 a:f ()~A491539608-------------------- Split Line---------------------2a:f ()~A (segment error)

Flag is 2 o'clock and debugging learns that the segment error occurred in line 47.

The following is a guess of the memory structure in this case:

Class UML (? Anyway, with argouml painting, the standard does not) Description:

Figure A. Memory structure guessing-figure B:

Vptr is a virtual pointer, the right side is the content of the virtual pointer function, that is, the effect of execution. is not the function body, this is just the entry address of the function, the output of the function is written for convenience, and the last 0 is the end flag of the virtual pointer table.


The memory model is figure B,

All execute a f () This virtual function at the end of the virtual function, the last is the end of the flag 0 (this flag does not represent all the end of the flag, some cases are 1, see the article began to recommend the article. Have the opportunity to sort out the situation of 1 and 0)

From flag 0 o'clock, the first "~a" is executed at the top of Figure B, and IA can print out to be 1, explaining what, and not saying for the time being (in fact, guessing)

By flag 1 o'clock, the second "~a" is executed in Figure B (top-down, not next), and IA is printed randomly. This stochastic conclusion can be obtained in the case of multiple executions.

By Flag 2 o'clock, a second "~a" is executed, and a break error occurs when executing "~a" at the top. With flag 0 o'clock Ia=1,flag to 1 o'clock IA results random, bold guess after executing the second "~a", Memory has been freed .

I adjusted the flag to 2 o'clock, first execute p[0][0], then execute p[0][1], there is no paragraph error. The output is as follows

A:f () ~a~a0x5b00c408


In this case the memory structure is shown in B, and multiple virtual functions are queued directly behind the virtual function table. And the second one is the real destructor. As for why there are two "~a", the principle I do not know, I guess maybe is wrong, I just see the phenomenon. My idea at the moment is to summarize what I can think of as a phenomenon of virtual destructors.

Note: I divided several categories for analysis, code written well, the conclusion will soon be out, but the article is really difficult to write, but also to let others understand. Of course it helps my own thinking. See you in the next episode.

Virtual destructor of C + + Object memory model (1)

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: 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.