Summary of Memory Distribution of classes with virtual tables

Source: Internet
Author: User

This problem has always been plausible. I had nothing to worry about yesterday. I will take a closer look and find it quite easy.

It can be summarized into the following parts:

1. Empty class Memory Distribution

2. Memory Distribution of classes with variables

3. Memory Distribution of classes with virtual functions

4. Memory Distribution of sub-classes with virtual functions

5. Description of virtual destructor

6. Why must pure virtual functions be implemented in sub-classes.

Unfinished part:

1. structure distribution of the virtual base class.

 

1. The memory distribution of the empty class is relatively simple. It is generally expressed in one byte. It is said that it is a special arrangement to identify the class. The following code:

Class {}

Sizeof (a) is 1.

2. If the class contains a variable, the class size is the variable size.

3. Once a class contains a virtual function, the class size is increased by four bytes. The first four bytes (for 32-bit machines) are the virtual table entry addresses, this address points to an array to store the address of the virtual function.

4. the array pointed to by the virtual table in the subclass. For unoverwritten virtual functions, the virtual function address of the parent class is directly followed. If it has already been overwritten, It is rewritten to the virtual function address of the subclass.

5. Virtual destructor are also virtual functions. At the end of the virtual table array, some parameters are added to the operating system during virtual destructor, so they cannot be called manually.

6. We know that all interfaces of pure virtual functions must be implemented in sub-classes. Otherwise, an error occurs in the program because pure virtual functions are not implemented in the parent class, the system points to an incorrect address. If some sub-classes are not implemented, the addresses will also be placed in the virtual table as follows, resulting in an error.

Finally, let's end with a simple example. The code can print the addresses of various variables and virtual tables:

#include "stdafx.h"#include < iostream >using namespace std;class Base {public:    Base() {        cout << "In Base" << endl;        cout << "Virtual Pointer = " << (int*)this << endl;        cout << "Address of Vtable = " << (int*)*(int*)this << endl;        cout << "Value at Vtable 1st entry = " << (int*)*((int*)*(int*)this+0) << endl;        cout << "Value at Vtable 2nd entry = " << (int*)*((int*)*(int*)this+1) << endl;        cout << endl;    }    int base_;    virtual void f1() = 0;    virtual void f2() = 0;};class MostDrive : public Base {public:    MostDrive() {        cout << "In MostDrive" << endl;        cout << "Virtual Pointer = " << (int*)this << endl;        cout << "Address of Vtable = " << (int*)*(int*)this << endl;        cout << "Value at Vtable 1st entry = " << (int*)*((int*)*(int*)this+0) << endl;        cout << "Value at Vtable 2nd entry = " << (int*)*((int*)*(int*)this+1) << endl;        cout << endl;    }    void test() {        cout << "------------------------test---------------------"<<endl;        cout << "In Drive" << endl;        cout << "Virtual Pointer = " << (int*)this << endl;        cout << "Address of Vtable = " << (int*)*(int*)this << endl;        cout << "Value at Vtable 1st entry = " << (int*)*((int*)*(int*)this+0) << endl;        cout << "Value at Vtable 2nd entry = " << (int*)*((int*)*(int*)this+1) << endl;        cout << endl;    }    virtual void f1() { cout << "MostDrive::f1" << endl; }    virtual void f2() { cout << "MostDrive::f2" << endl; }    int mostdrive_;};typedef void(*Fun)();int main() {    MostDrive d;    d.base_ = 1;    d.mostdrive_ = 3;    d.test();    cout << "------------in main()-----------------------";    cout << "sizeof(MostDrive)=" <<sizeof(MostDrive) << endl;    cout << "Virtual Pointer = " << (int*)&d << endl;    cout << "Address of Vtable = " << (int*)*(int*)&d << endl;    cout << "Value at Vtable 1st address = " << ((int*)*(int*)&d+0) << endl;    cout << "Value at Vtable 1st entry = " << (int*)*((int*)*(int*)&d+0) << endl;    cout << "Value at Vtable 2nd address = " << ((int*)*(int*)&d+1) << endl;    cout << "Value at Vtable 2nd entry = " << (int*)*((int*)*(int*)&d+1) << endl;    Fun pFun1 = (Fun)*((int*)*(int*)&d+0);    pFun1();    Fun pFun2 = (Fun)*((int*)*(int*)&d+1);    pFun2();    cout << "first value address = " <<(int*)(int*)&d + 1 <<endl;    cout << "first value = " << *((int*)&d + 1) << endl;    cout << "second value address = " <<(int*)(int*)&d + 2 <<endl;    cout << "second value = " << *((int*)&d + 2) << endl;    return 0;}

Running result:

In base
Virtual pointer = 0012ff58
Address of vtable = 004184d8
Value at vtable 1st entry = 004111db
Value at vtable 2nd entry = 004111db

In mostdrive
Virtual pointer = 0012ff58
Address of vtable = 00417940
Value at vtable 1st entry = 00411145
Value at vtable 2nd entry = 004110c8

------------------------ Test ---------------------
In drive
Virtual pointer = 0012ff58
Address of vtable = 00417940
Value at vtable 1st entry = 00411145
Value at vtable 2nd entry = 004110c8

------------ In main () --------------------- sizeof (mostdrive) = 12
Virtual pointer = 0012ff58
Address of vtable = 00417940
Value at vtable 1st address = 00417940
Value at vtable 1st entry = 00411145
Value at vtable 2nd address = 00417944
Value at vtable 2nd entry = 004110c8
Mostdrive: F1
Mostdrive: F2
First value address = 0012ff5c
First value = 1
Second value address = 0012ff60
Second value = 3

The figure is as follows:

 

 

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.