Memory layout of C + + objects in VS

Source: Internet
Author: User

This article focuses on the memory layout of C + + objects in Visual Studio, where there is no test code, just a graphical representation of the memory distribution, the code for testing, and C + + Other elements of the object model you can refer to Mr. Chenhao's several blog posts and other articles on the Web:

"C + + virtual function table parsing": http://blog.csdn.net/haoel/article/details/1948051

Memory layout (top) of C + + objects: http://blog.csdn.net/haoel/article/details/3081328

"Memory layout of C + + objects (bottom)": http://blog.csdn.net/haoel/article/details/3081385

Based on my own debug results (release version), the principle of VS processing objects can be broadly divided into the following points:

1, for ordinary member variables, according to the Order of Declaration and the principle of memory alignment stored.

For example, Class A, which occupies 8 bytes in a 32-bit program:

class a{public:    void Test () {cout << "a::test\n";} Private :     int NA;     Char ChA;};

The layout in memory is as follows:

2. If the object contains a virtual function, add a pointer at the beginning of the object that points to a virtual function table, in which the address of the virtual function in the class is stored sequentially.

For example, Class A, which occupies 12 bytes in a 32-bit program:

class a{public:    virtualvoid f () {cout << "a::f () \ n";    } Virtual void FA () {cout << "A::fa () \ n";} Private :     int NA;     Char ChA;};

The layout in memory is as follows, note that the last item of the virtual table is not necessarily 0:

3, if the object has only direct inheritance (non-virtual inheritance) of the parent class, according to the subclass and the parent class has no virtual function can be divided into the following situations:

<1>: Both the subclass and the parent have no virtual functions, first place each parent part in the order of inheritance, and then place the sub-class part;

For example, Class B, which occupies 24 bytes in a 32-bit program:

classa1{Private:    intnA1; CharchA1;};classa2{Private:intnA2;CharchA2;};classD | PublicA1, Publica2{Private:intNB;CharChB;};

The layout in memory is as follows:

<2>: The subclass has virtual function, the parent class has no virtual function, then the virtual function table pointer of the subclass is placed, then the parent class part is placed, and then the subclass part is placed;

For example, Class B, which occupies 20 bytes in a 32-bit program:

classa{Private:    intNA; CharChA;};classD | Publica{ Public:    Virtual voidF () {cout <<"B::f () \ n";} Virtual voidFB () {cout <<"B::FB () \ n";}Private:intNB;CharChB;};

The layout in memory is as follows:

<3>: Both the subclass and the parent class have virtual functions, first put the parent class with the virtual function in the parent class list before, then place the parent class without the virtual function, then place the subclass part (no virtual function pointer), and modify each virtual function table and pointer. Make it meet the following conditions: The first virtual function table pointer to the virtual function table is placed before inheriting from the parent class virtual function address, including as-is inherited and rewritten, and then place the subclass unique virtual function address, the rest of the virtual function table pointers to the virtual function table contains only the virtual function address inherited from the corresponding parent class, Including the inherited and rewritten.

For example, Class B, which occupies 40 bytes in a 32-bit program:

classa1{Private:    intnA1; CharchA1;};classa2{ Public:    Virtual voidF () {cout <<"A2::f () \ n";} Virtual voidFA2 () {cout <<"A2::FA2 () \ n";}Private:    intnA2; CharchA2;};classa3{ Public:    Virtual voidF () {cout <<"A3::f () \ n";} Virtual voidFA3 () {cout <<"A3::fa3 () \ n";}Private:    intnA3; CharchA3;}; classD | PublicA1, PublicA2, Publica3{ Public:Virtual voidF () {cout <<"B::f () \ n";}Virtual voidFA3 () {cout <<"B::fa3 () \ n";} Virtual voidFB () {cout <<"B::FB () \ n";}Private:intNB;CharChB;};

The layout in memory is as follows:

4, ( This is not quite certain ) if the object has a virtual base class, whether it is inherited by itself or the virtual inheritance of the parent class, then the non-virtual base class part is processed in accordance with the above rules, then insert a pointer, and then put the rest of the class member variable (the pointer to a table, Each item in the table is a 32-bit signed integer-whether it is a 32-bit program or a 64-bit program, where the first item is the offset of the pointer to the header of this class, followed by the offset of the pointer to the starting position of the virtual base class, and then the virtual base class is placed. One problem here is that sometimes a full-zero pointer is placed in front of the virtual base class, but sometimes it is not, and according to the results of my current test, when a subclass has a constructor and simply overrides a function of the virtual base class, the virtual base class will have this all-zero pointer in front of it.

For example, the following programs occupy space in 32-bit programs, respectively:

classa{ Public:    Virtual voidF () {cout <<"a::f () \ n"; } Virtual voidFA () {cout <<"a::fa () \ n"; }Private:    intNA; CharChA;};classD | Public Virtuala{ Public:    Virtual voidF () {cout <<"b::f () \ n"; } Virtual voidFB () {cout <<"B::FB () \ n"; }Private:    intNB; CharChB;};classc{ Public:    Virtual voidF () {cout <<"c::f () \ n"; } Virtual voidFC () {cout <<"C::FC () \ n"; }Private:    intNC; CharChC;};classd{ Public:    Virtual voidF () {cout <<"d::f () \ n"; } Virtual voidFD () {cout <<"d::fd () \ n"; }Private:    intND; CharChD;};classe{ Public:    Virtual voidF () {cout <<"e::f () \ n"; } Virtual voidFE () {cout <<"e::fe () \ n"; }Private:    intNE; CharChE;}; classF: Public VirtualB Public VirtualC PublicD Public Virtuale{ Public: F () {}Virtual voidF () {cout <<"f::f () \ n"; } Virtual voidFB () {cout <<"F::FB () \ n"; } Virtual voidFC () {cout <<"F::FC () \ n"; } Virtual voidFE () {cout <<"f::fe () \ n"; } Virtual voidFF () {cout <<"f::ff () \ n"; }Private:    intNF; CharChF;};

A, C, D, e are 12 bytes, take a for example, the memory layout is as follows:

B is 28 bytes, in memory layout as follows, it is worth noting that at this point , b because the virtual base class a is already overridden by a virtual function f (), So if you show the definition of a constructor again, a full 0 pointer will be added before part A of B . But if the constructor or the rewrite of F () is omitted, the full 0 pointer will not exist :

F occupies 92 bytes, in the memory layout, where the order of the virtual base class is in the order of the list, for example, in this case, F virtual inheritance has B, C, E three classes, so in the virtual base class emission sequence first put B, and because B virtual inherit a, so the final order is a, B, C, E. Another point is that because F defines a constructor, there is a full 0 pointer in front of the virtual base class, and if you remove the constructor, the size of F becomes 76 bytes, which is exactly 92 bytes minus 4 All 0 pointers in size:

Memory layout of C + + objects in VS

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.