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

Source: Internet
Author: User

The second situation is now discussed:

(first case portal, single class, with virtual destructor, virtual function)

(2) There are inheritance relations, single inheritance, the parent class no virtual destructor, subclass has (subclass no need to say)

This kind of situation makes me quite dizzy, the usual first code:

1#include <iostream>2 using namespacestd;3 4 classA5 {6        intia;7         Public:8A (): IA ( the)9      {        Ten       }  One~A () A      { -cout <<"~a"<<Endl; -      }  the      Virtual voidf () -      { -cout <<"a:f ()"<<Endl; -      }  + }; -  + classD | PublicA A { at        intIB; -  Public : -B (): IB ( to){} -       Virtual~B () -       { -cout <<"~b"<<Endl;  in       }   -       Virtual voidf () to       { +cout <<"b:f ()"<<Endl;  -       } the         * }; $ Panax Notoginsengtypedefvoid(*F) (); -  the intMain () + { A  theF PF =NULL; +B *b =NewB (); -     int**p = (int**) b; $  $     intFlag =0; -PF = (F) p[0][flag]; - pf (); the  -cout <<"END"<<p[0][3] <<Endl;Wuyicout << p[1] <<Endl; thecout << p[2] <<Endl; -  Wucout <<sizeof(A) <<Endl; -cout <<sizeof(B) <<Endl; About}

The output is as follows:

Flag = 0;

1 b:f () 2 END0 3 0xf 4 0x1f 5 8 6  A

flag = 1;

1 ~B2 ~A3END446035640xf5  0x1f687

flag = 2;

~B~AEND18188467810x2c29a00x1f8

Analysis:

Flag=0, the virtual function F of B replaces the virtual function F of B, the PrintOut b:f (), which is a good understanding. The point to note is that this time to flag=0 means that f is at the top of the virtual function table and a does not have a virtual destructor. The number after end is also normal.

Flag=1, the output is ~b\n~a\n, from the phenomenon, is to perform the destructor of B and then perform a destructor, and common sense. But the data after end is different from the data 0 when flag=0, plus the value of the two member variables in B is right, which makes it hard to believe that the object memory is freed. If not released, the number after end is difficult to explain.

flag=2, the output is ~b\n~a\n, the member variable output is garbled, at least the memory is released. IA is garbled, IB is still 0x1f. From the output point of view, successively executed the B,a destructor, confirming that the memory has been released. Since IB is still 0x1f, I think it is the reason that memory is not updated.

Conclusion: As stated in the first article, the second of the virtual destructor is to release memory, and the first one still does not know what to do. The rest of the two connections that were introduced at the beginning of the article say that the virtual function of a subclass overrides the position of the parent class in the virtual function table.

Here's a memory structure:

A's memory structure please see the first article, where only B is drawn.

Vptr is a virtual function of the table pointer, the picture is said, of course, these are all based on the phenomenon of guessing, at present, not to see the authoritative information reason is to really understand, now see how much they can guess. No one scored me, afraid of fur.

Note: The class diagram can be used ARGOUML, the memory map can be used win7 drawing tools.

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

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.