A deep understanding of the C ++ object model and a deep understanding of the c Object Model

Source: Internet
Author: User

A deep understanding of the C ++ object model and a deep understanding of the c Object Model

The C ++ object model is an important knowledge point. It learns the memory model of the C ++ object, you can understand the polymorphism principle in C ++, the class initialization sequence, and the class size.

1 C ++ object model basics What are there in 1.1 C ++ objects?

The C ++ object includes the following:

  • Static Constants
  • Member variables
  • Member Functions
  • Virtual Functions
  • Pure Function
  • ...

The following is an object definition:

class Base{    static int b_s;public:    void function() { }    virtual void v_function() {        cout << "Base v_function()" << endl;    }private:    int b_a;    int b_b;};
1.2 What is the size of a C ++ object?

If the Base class is used for testing, how many bytes does the Base class occupy in the memory?

Window7 vs2013 test results:

Centos7 64-bit vim Test Result:

The above two test results show that the memory usage of a class is determined by the following Members:

  • Total size of non-static members
  • Add any space to be filled (padding) due to alignment requirements
    • If the class has only non-static members, such as char c, the size is 1.
    • If there is int a in addition to char c;, the size is 8.
  • Added the internal extra burden to support virtual
Some important syntactic sugar in 1.3 C ++
  • Static constant integer member (double won't work) is directly initialized within the class
  • Static members can only be initialized outside the class, and static members are not added during initialization.
  • The base class is enough to call the virtual function in the constructor to actually call the virtual function in the base class (this is different from Java)
  • Const member function: do not modify the class member data

 

2 C ++ Object Memory Layout

The test code is as follows:

#include <iostream>using namespace std;class Base{    static int b_s;public:    void function() { }    virtual void v_function() {        cout << "Base v_function()" << endl;    }    int b_a;    int b_b;};int Base::b_s = 0;int main(int argc, char **argv){    Base base;    base.b_a = 1;    base.b_b = 2;    cout << "size: " << sizeof(base) << endl;    int *p = (int *) &base;    cout << *p << endl;    p++;    cout << *p << endl;    p++;    cout << *p << endl;    system("pause");    return 0;}

Output result:

In the output result, values 1 and 2 are values of B _a and B _ B members in the class. 11459700 indicates an address. The modified address contains information about the virtual table. The general memory layout of the Base class is as follows:

2.1 What is the size of an empty class?
class Empty { };int main(int argc, char **argv){    Empty empty;    Empty emptys[10];    cout << sizeof(empty) << endl;    cout << sizeof(emptys) << endl;    system("pause");    return 0;}

Output result:

There is nothing in the empty class, but a variable (Instance) of the empty class type is defined. Each instance has a unique address in the memory. To achieve this, the compiler often implicitly adds a byte to an empty class, so that the empty class gets a unique address in the memory after instantiation.

2.2 class initialization relationships under the C ++ Object Inheritance System

(1) class initialization sequence from the perspective of space

Base class initialization-subclass Initialization

(2) class initialization sequence from the air perspective

Base Class static member-subclass static member-base class member variable-base class constructor-subclass member variable-subclass Constructor

(3) stand on the ground and check the class initialization sequence

Base Class static member-subclass static member-(set v_ptr/base class member variable)-base class constructor-(set v_ptr/subclass member variable)-subclass Constructor

 

3. Virtual table 3.1 virtual table pointer and virtual table structure in the C ++ class

3.2 virtual tables under multiple inheritance

Multi-inheritance supports Virtual functions. The complexity of these functions is centered on the second and subsequent base classes, and the "this pointer must be adjusted during the execution period ".

class Base1 {public:    Base1();    virtual ~Base1();    virtual void speakClearly();    virtual Base1 *clone() const;protected:    float data_Base1;};class Base2 {public:    Base2();    virtual ~Base2();    virtual void number();    virtual Base2 *clone() const;protected:    float data_Base2;};class Derived : public Base1, public Base2 {public:    Derived();    virtual ~Derived();    virtual Derived *clone() const;protected:    float data_Derived;};

3.3 virtual table structure under virtual inheritance

For details about virtual inheritance, click: Virtual inheritance-Baidu encyclopedia.

class Point2d {public:    Point2d(float = 0.0, float = 0.0);    virtual ~Point2d();    virtual void mumble();    virtual float z();    // ... other codeprotected:    float _x, _y;};class Point3d : virtual public Point2d {public:    Point3d(float = 0.0, float = 0.0, float = 0.0);    ~Point3d();    float z();protected:    float _z;};

3.4 When is the virtual table pointer assigned?
#include <iostream>using namespace std;class Base{public:    Base() {        cout << "Base()" << endl;        show();        int *p = &b;        cout << "Base::b: " << p << endl;        p--;        cout << "Base::vptr: " << *p << endl;        cout << "*Base::vptr: " << *(int *)*p << endl;        cout << endl;    }    virtual void show() {        cout << "Base::show()" << endl;    }public:    int b;};class Derived : public Base{public:    Derived()    {        cout << "Derived()" << endl;        show();        int *p = &b;        cout << "Derived::b: " << p << endl;        p--;        cout << "Derived::vptr: " << *p << endl;        cout << "*Derived::vptr: " << *(int *)*p << endl;        cout << endl;    }    virtual void show() {        cout << "Derived::show()" << endl;    }private:    int d;};int main(int argc, char **argv){    Base base;    Derived derived;    system("pause");    return 0;}

Output result:

From the output result, we can conclude that during the construction of the subclass, the virtual table pointer will be assigned twice. The initialization is as follows:

Base Class static member-subclass static member-(set v_ptr/base class member variable)-base class constructor-(set v_ptr/subclass member variable)-subclass Constructor

 

Refer:

1. in-depth exploration of the C ++ Object Model

2. The c ++ empty class instance size is not 0

 

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.