Simple Object Model of C ++ Object Model (2)

zhaozj2021-02-17  50

1.3. Objects containing methods

What is the object of the method, what should its size and memory layout look like? Let us take a look.

1.3.1. Objects that do not include virtual functions

Investigate the following classes:

Class SIMPLE

{

PUBLIC:

Simple (char _a, int _i);

Private:

Char a;

INT I;

}

What should its size? How much should the function take up in data occupied by 8 bytes? Let's do a test. This time is amazing that its size has not changed, still 8. Then its memory layout should be

Char a (1 byte)

Suppose the placement (3 byte)

INT i (4 byte)

Simple (char _a, int _i)

SIMPLE object memory layout

What is the memory layout? Let us design a program to verify.

0001 #include

0002 #include

0003 Using Namespace STD;

0004 / / ----------------------------------- -----------------

0005 Class Simple

0006 {

0007 public:

0008 Simple (Char _a, INT_I);

0009 private:

0010 char A;

0011 INT I;

0012};

0011 / / ----------------------------------------------- -----------------

0014 Simple :: Simple (Char_A, INT _i)

0015 {

0016 a = _A;

0017 i = _i;

0018}

0019 / / ----------------------------------------------- -----------------

0020 int Main (int Argc, char * argv [])

0021 {

0022 cout << "SIZEOF (Simple" << '/ t' << sizeof (simple) << Endl;

0023 Simple A ('C', 9);

0024 INT * IP = (int *) & a;

0025 char * cp = (char *) & a;

0026 cout << "simple.a" << '/ t' << * cp << endl;

0027 cout << "Simple.i" << '/ t' << * ip << Endl;

0028 getCH ();

0029 return 0;

0030}

0031 / / ------------------------------------------------- -----------------

The result is in line with our guess.

1.3.2. Include objects with virtual functions

Investigate the following classes:

Class SIMPLE

{

PUBLIC:

Simple (char _a, int _i);

Virtual void vprint (void);

Void Print (Void);

Private:

Char a;

INT I;

}

How much is this size? According to the above experience, the function does not occupy the storage space of the object, then his size should be 8. Isn't it right? Take it again. How did the results become "12"? Slow, let us think about it, virtual functions? What does it mean? The virtual function means polymorphism, that is, which function is decided to call the call. So how do you achieve this? The answer is Virtual Function Table, which is the usual virtual function table. With the indirect reference to the Virtual Function Table, we can decide which function calls during the run. So how can I get Virtual Function Table? This requires a pointer called VTBL, and this pointer points to the start address of the Virtual Function Table, then the call to the virtual function becomes a reference to the Virtual Function Table, the function pointer is called. Of course, this will be an efficient price for the execution period. The object model containing virtual functions is as follows:

Void vprint (void)

Char a (1 byte)

Suppose the placement (3 byte)

INT i (4 byte)

VTBL (4 byte)

Data layout virtual function table

Simple (char _a, int _i)

Void Print (Void)

SIMPLE object memory layout

Let us make a verification

0001 #include

0002 #include

0003 Using Namespace STD;

0004 / / ----------------------------------- -----------------

0005 Class Simple

0006 {

0007 public:

0008 Simple (Char _a, INT_I);

0009 Virtual Void Vprint (Void);

0010 Void Print (Void);

0011 private:

0012 char A;

0013 INT I;

0014};

0015 / / ----------------------------------------------- -----------------

0016 Simple :: Simple (Char_A, INT _i)

0017 {

0018 a = _A;

0019 i = _i;

0020}

0021 / / ----------------------------------------------- -----------------

0022 Void Simple :: Print (void)

0023 {

0024 cout << "simple.a" << a << '/ t' << "simple.i" << i << ENDL;

0025}

0026 / / ----------------------------------------------- -----------------

0027 Void Simple :: Vprint (void)

0028 {

0029 COUT << "Simple.a" << a << '/ t' << "Simple.i" << i << Endl

0030}

0031 / / ------------------------------------------------- -----------------

0032 Int Main (int Argc, char * argv [])

0033 {

0034 COUT << "SIZEOF (Simple" << '/ t' << sizeof (simple) << ENDL;

0035 Simple A ('C', 9);

0036 INT * IP = (int *) & A;

0037 char * cp = (char *) & a;

0038 cout << "simple.a" << '/ t' << * CP << endl;

0039 cout << "Simple.i" << '/ t' << * ip << Endl;

0040 cout << "Simple.vtbl" << '/ t' << * IP << Endl;

0041 getCH ();

0042 returnography;

0043}

0044 / / ----------------------------------------------- -----------------

The output of the program is:

Sizeof (Simple 12)

Simple.a c

SIMPLE.I 9

SIMPLE.VTBL 4269384

Very else in line with our speculation. The above is the compilation result of GCC, on C Builder, code and results are slightly different.

COUT << "SIZEOF (Simple" << '/ t' << sizeof (simple) << endl;

Simple A ('C', 9);

INT * IP = (int *) & a;

Char * cp = (char *) & a;

Cout << "Simple.vtbl" << '/ t' << * IP << Endl;

COUT << "Simple.a" << '/ t' << * (CP 4) << ENDL;

Cout << "Simple.i" << '/ t' << * (IP 2) << ENDL;

The output of the program is:

Sizeof (Simple 12)

SIMPLE.VTBL 4207548

Simple.a c

SIMPLE.I 9

Different situations

VTBL

At the beginning of the object memory layout or end.

转载请注明原文地址:https://www.9cbs.com/read-29285.html

New Post(0)