C + + Object Memory Distribution (3)-Diamond Inheritance (virtual)

Source: Internet
Author: User

1. Preface

For all the code examples in this article, if you are running on Windows compilation, you are using Visual Studio 2013. If the RHEL6.5 platform (Linux kernal:2.6.32-431.el6.i686) is compiled and run, its GCC version is 4.4.7 as follows:
[Email protected] ~]# gcc--version
GCC (gcc) 4.4.7 20120313 (Red Hat 4.4.7-4)

2. Memory distribution of a diamond-inheriting class

This article mainly discusses the memory distribution under virtual inheritance (virtual).

2.1. Structure of the class


2.2. Implementing the Code Windows version

The following code runs on the Windows 7+visual Studio 2013 platform.

Prints the memory distribution of the class Base1 object, and the memory distribution of the class derive object, respectively.

#include <iostream> using namespace Std;class Base {public:int _ibase;char _cbase;public:base (): _ibase (1111), _CB ASE (' A ') {}virtual void func () {cout << "Base::func ()" << Endl;} virtual void Basefunc () {cout << "Base::basefunc ()" << Endl;}}; Class Base1:public virtual Base {public:int _ibase1;char _cbase1;public:base1 (): _ibase1 (2222), _cbase1 (' B ') {}virtual V OID func () {cout << "Base1::func ()" << Endl;} virtual void func1 () {cout << "base1::func1 ()" << Endl;} virtual void BaseFunc1 () {cout << "base1::basefunc1 ()" << Endl;}}; Class Base2:public Virtual Base{public:int _ibase2;char _cbase2;public:base2 (): _ibase2 (3333), _cbase2 (' C ') {}virtual V OID func () {cout << "Base2::func ()" << Endl;} virtual void Func2 () {cout << "base2::func2 ()" << Endl;} virtual void BaseFunc2 () {cout << "base2::basefunc2 ()" << Endl;}}; Class Derive:public virtual Base1, public virtual Base2{public:int _iderive;char_cderive;public:derive (): _iderive (4444), _cderive (' D ') {}virtual void func () {cout << "Derive::func ()" << Endl;} virtual void func1 () {cout << "derive::func1 ()" << Endl;} virtual void Func2 () {cout << "derive::func2 ()" << Endl;} virtual void Derivefunc () {cout << "Derive::d erivefunc ()" << Endl;}; typedef void (*fun) (); int main () {int **pvtab = NULL; Fun Pfun = NULL; Base1 bb1;cout << sizeof (base) << sizeof (base) << endl;cout << "sizeof (BASE1)" << sizeof ( BASE1) << endl;//base1 layoutpvtab = (int**) &bb1;cout << "[0] base1::_vptr->" << "addr:" &LT;&L T &pvtab[0] << Endl;pfun = (fun) pvtab[0][0];cout << "[0]";p fun (); BASE1::FUNC1 () Pfun = (fun) pvtab[0][1];cout << ' [1] ';p fun ();  BASE1::BASEFUNC1 () cout << "[2]"; cout << pvtab[0][2] << endl;cout << "[1] base1::_vbptr->" << "addr:" << &pvtab[1] << endl;cout &LT;&LT "[0]"; cout << pvtab[1][0] << endl;cout << "[1]offset->"; cout << pvtab[1][1] << en Dl;cout << "[2]"; cout << pvtab[1][2] << endl;cout << "[2] base1:_ibase1"; cout << (int) pvtab[2] << Endl; Base1:_ibase1cout << "[3] base1:_cbase1"; cout << (char) (int) pvtab[3] << Endl; Base1:_cbase1cout << "[4] unknown"; cout << (int) pvtab[4] << endl;cout << "[5] Base::_vfptr-&gt ; "<<" addr: "<< &pvtab[4] << endl;cout <<" [0] ";p fun = (fun) pvtab[5][0];p fun (); cout &lt ;< "[1]";p fun = (fun) pvtab[5][1];p fun (); cout << "[2]"; cout << (int) pvtab[5][2] << Endl;cout & lt;< "[6] base:_ibase, cout << (int) pvtab[6] << Endl; Base:_ibasecout << "[7] base:_cbase"; cout << (char) (int) pvtab[7] << Endl; Base:_cbasecout << endl;cout << "###########################" << endl;cout << endl;//derive layout//Sub Derive vtbl layoutderive d;cout << sizeof (Derive) << s Izeof (Derive) << Endl;pvtab = (int**) &d;cout << "[0] derive::_vfptr->" << "addr:" << &amp ;p vtab[0] << endl;cout << "[0]";p fun = (fun) pvtab[0][0];p fun (); DERIVE::FUNC1 () cout << "[1]"; cout << pvtab[0][1] << endl;cout << "[1] derive::_vbptr-> "<<" addr: "<< &pvtab[1" << endl;cout << "[0]"; cout << pvtab[1][0] << End L;cout << "[1]offset->"; cout << pvtab[1][1] << endl;cout << "[2]"; cout << pVt AB[1][2] << endl;cout << "[3]"; cout << pvtab[1][3] << endl;cout << "[4]"; cout &L t;< pvtab[1][4] << endl;cout << "[2] derive::_iderive"; cout << (int) pvtab[2] << Endl; Derive:_iderivecout << "[3] derive::_cderive"; cout<< (char) (int) pvtab[3] << Endl; Derive::_cderivecout << "[4] unknown->" << (int) pvtab[4] << endl;//Sub Base vtbl layoutcout <& Lt "[5] base::_vfptr->" << "addr:" << &pvtab[5] << endl;cout << "[0]";p fun = (fun) Pvta B[5][0];p fun ();p fun = Pvtab[5][1];cout << "[1]";p fun () cout << [2]; cout << pvtab[5][2 ] << endl;cout << [6] base::_ibase, cout << (int) pvtab[6] << endl;cout << "[7] Base: : _cbase, cout << (char) (int) pvtab[7] << endl;cout << "[8] unknown->"; cout << (int) pvtab  [8] << endl;//Sub Base1 vtbl layoutcout << "[9] base1::_vfptr->" << "addr:" << &pvtab[9]     << endl;cout << "[0]"; cout << pvtab[9][0] << Endl;pfun = (fun) pvtab[9][1];cout << " [1] ";p fun ();p fun = (fun) pvtab[9][2];cout <<" [2] "; cout << (int) Pfun << Endl;pfun = (fun) pvtab[9][3];cout << "[3]"; cout << (int) pfun << endl;cout << "[Ten] unknown- > "; cout << (int) pvtab[10] << endl;cout <<" [one] base1::_ibase1 "; cout << (int) pvtab[11] << endl;cout << [base1::_cbase1], cout << (char) (int) pvtab[12] << endl;cout << "[ Unknown-> "cout << (int) pvtab[13] << endl;//Sub Base2 vtbl layoutcout <<" [+] BASE2::_VFPTR-&G  t; "<<" addr: "<< &pvtab[14] << endl;cout <<" [0] "; cout << Pvtab[14][0] << Endl;pfun = (fun) pvtab[14][1];cout << ' [1] ';p fun (); cout << "[2]" cout << pvtab[14][2] <& Lt Endl;cout << "[3]"; cout << pvtab[14][3] << endl;cout << "[] unknown->"; cout << ( int) pvtab[15] << endl;cout << [Base2::_ibase2], cout << (int) pvtab[16] << endl;cout &lt ;< "[+] Base2::_CBase2, cout << (char) (int) pvtab[17] << Endl;return 0;} 

The memory distribution of the Base1 object is:


The memory distribution of the derive object is:


2.3. Implementing the Code Linux version

In the code below, the definitions for these 4 classes are exactly the same as those in the previous Windows code, so this is omitted. Only the code that is used to output the memory distribution is listed:

int main () {int **pvtab = NULL; Fun Pfun = NULL; Base1 bb1;cout << sizeof (base) << sizeof (base) << endl;cout << "sizeof (BASE1)" << sizeof ( BASE1) << endl;//base1 layoutpvtab = (int**) &bb1;cout << "[0] base1::_vptr->" << "addr:" &LT;&L T &pvtab[0] << Endl;pfun = (fun) pvtab[0][0];cout << "[0]";p fun (); BASE1::FUNC1 () Pfun = (fun) pvtab[0][1];cout << ' [1] ';p fun (); BASE1::BASEFUNC1 () Pfun = (fun) pvtab[0][2];cout << "[2]";p fun (); cout << "[3]"; cout << pvtab[0][  3];cout << "[1] base1:_ibase1"; cout << (int) pvtab[1] << Endl;  Base1:_ibase1cout << "[2] base1:_cbase1"; cout << (char) (int) pvtab[2] << Endl; base1:_cbase1//Base layoutcout << "[3] base::_vptr->" << "addr:" << &pvtab[3] << Endl; Pfun = (fun) pvtab[3][0];cout << ' [0] ';p fun ();   Base1::func () Pfun = (fun) pvtab[3][1];cout << " [1] ";p fun (); Base::basefunc () cout << "[2]"; cout << pvtab[3][2] << endl;cout << "[4] base:_ibase"; c Out << (int) pvtab[4] << Endl; Base:_ibasecout << "[5] base:_cbase"; cout << (char) (int) pvtab[5] << Endl; Base:_cbasecout << "[6]" << (int) pvtab[6] << "addr:" << &pvtab[6] << Endl; Dirty Datacout << endl;cout << "###########################" << endl;cout << endl;//Derive Lay out//Sub Derive vtbl layoutderive d;cout << sizeof (Derive) << sizeof (Derive) << Endl;pvtab = (int**) &     Amp;d;cout << "[0] derive::_vfptr->" << "addr:" << &pvtab[0] << endl;cout << " [0] ";p fun (); DERIVE::FUNC1 ();p fun = (fun) pvtab[0][1];cout << "[1]";p fun (); BASE1::BASEFUNC1 ();p fun = (fun) pvtab[0][2];cout << "[2]";p fun (); Derive::d erivefunc ();p fun = (fun) pvtab[0][3];cout << "[3]";Pfun () cout << "[4]"; cout << pvtab[0][4] << endl;cout << "[1] derive:_iderive"; Cout & lt;< (int) pvtab[1] << Endl; Derive:_iderivecout << "[2] derive:_cderive"; cout << (char) (int) pvtab[2] << Endl; derive:_cderive//Sub Base1 vtbl layoutcout << "[3] base1::_vptr->" << "addr:" << &pvtab[3] & lt;< Endl;pfun = (fun) pvtab[3][0];cout << ' [0] ';p fun ();p fun = (fun) pvtab[3][1];cout << "[1]";p f  Un ();p fun = (fun) pvtab[3][2];cout << "[2]";p fun (); cout << "[3]"; cout << Pvtab[3][3] << Endl;cout << "[4] base1::_ibase1"; cout << (int) pvtab[4] << endl;cout << "[5] Base1::_cbase 1, "; cout << (char) (int) pvtab[5] << endl;//Sub Base vtbl layoutcout <<" [6] base::_vptr-> "< < "addr:" << &pvtab[6] << Endl;pfun = (fun) pvtab[6][0];cout << "[0]";p fun ();p fun = (fun) pVtab[6][1];cout << "[1]";p fun ();p fun = (fun) pvtab[6][2];cout << "[2]"; cout << (int) Pfun < < Endl;cout << [7] base::_ibase, cout << (int) pvtab[7] << endl;cout << "[8] Base::_cbas E--"; cout << (char) (int) pvtab[8] << endl;//Sub Base2 vtbl layoutcout <<" [9] base2::_vptr-> "&L t;< "addr:" << &pvtab[9] << Endl;pfun = (fun) pvtab[9][0];cout << "[0]";p fun ();p fun     ) Pvtab[9][1];cout << "[1]";p fun ();p fun = (fun) pvtab[9][2];cout << "[2]";p fun (); cout << " [3] "; cout << pvtab[9][3] << endl;cout <<" [Ten] Base2::_ibase2 "; cout << (int) pvtab[10] &lt ;< endl;cout << "[one] base2::_cbase2"; cout << (char) (int) pvtab[11] << Endl;return 0;}
the memory distribution of the Base1 object is:


The memory distribution of the derive object is:

memory Distribution Comparison of 2.4.BASE1 class

Windows 7 + Visual Studio Linux RHEL6.5 + gcc 4.4.7


memory Distribution Comparison of 2.5.Derive classes

Windows 7 + Visual Studio Linux RHEL6.5 + gcc 4.4.7


Memory comparison of 2.6.Derive classes in virtual and non-virtual inheritance

In the previous section (2.5), the detailed memory distribution of derive under Windows and Linux platforms is listed in virtual Inheritance (virtual).
In the previous article, we obtained the memory distribution of the derive class in the case of non-virtual inheritance (non virtual). Here is a comparison of the distribution of virtual inheritance obtained in this article.


is a comparison of the effects of non-virtual inheritance and virtual inheritance on the memory distribution of a class under the Windows platform.

Non-virtual inheritance (non virtual) virtual inheritance (virtual)


is a comparison of the effects of non-virtual inheritance and virtual inheritance on the memory distribution of a class under the Linux platform.

Non-virtual inheritance (non virtual) virtual inheritance (virtual)


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + Object Memory Distribution (3)-Diamond Inheritance (virtual)

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.