Memory layout for C + + objects

Source: Internet
Author: User

Recently writing a C + + and Java Socket Communication program, I put the entire struct directly through the socket, the client needs to interpret the received byte data, extending the layout of C + + objects, I read the "Deep Exploration of C + + object Model" of some content, combined with their own understanding , organized as follows.

There are two keywords for class in C + +, struct and class,struct are for compatibility with C language, class represents object-oriented thought. I remember that the C + + Primer wrote that the struct and class are the same in C + +, the only difference being that the default access level for a struct is public, and class is private.

The C language also has a struct that can define a new data type by itself, but it does not have a true object-oriented mechanism (although it can be programmed with similar object-oriented thinking). C + + 's struct is object-oriented, that is, encapsulation (data hiding), inheritance and polymorphism. There is no mechanism for C-language structs to support inheritance and polymorphism.

First, what is memory alignment , Many computer systems have restrictions on the location of the basic type of data in memory, they will require that the value of the first address of the data is a number K ( usually it is 4 or 8) a multiple, K is called the number of Zimo, This is called memory alignment. If one alignment of K is another integer multiple (>1), then the alignment of the larger k is more stringent, because there are fewer places to place. Different compilers are not necessarily the same for module settings, and the modulus of different types is often different. Microsoft's Win32 compiler, the base data type of the Zimo number is the size of that type, sizeof (Primarytype), the compiler's alignment options can also be set. (This paragraph was excerpted from http://blog.csdn.net/soloist/article/details/213717)

About the memory layout of C struct

Can be seen as part of the C + + object layout rule, and the parts of C + + struct removal to support object-oriented overhead are consistent.

I understand that C struct is a few data "save" together, form a whole, no other things

such as struct point{int x, y, z;} p; In memory P is 3 int value, "save" together, no other data.


Figuring out the layout is about sizing and positioning , and there are a few standards that define the problem.

The Ansic standard guarantees that the positions of each field in the struct are incremented in memory as they are declared.

ANSIC specifies that the size of a struct type is the sum of the size of all its fields and the size of the fill area between fields or the end of a field. The fill area guarantees that each field of a struct conforms to its own memory alignment requirements.

Ansic The standard specifies that the alignment requirement for a struct type cannot be more restrictive than the one that is most stringent in all of its fields. This is the first address of the whole structure.

So although the ANSI C standard makes some provisions, but for the alignment of the data part, or depends on the compiler, so the same struct under different compiler/different compiler to the alignment key parameter, its memory layout may be different.

suppose the int type is 4 bytes long, char type 1 bytes, short type 2 bytes, each type according to the respective size as to Zimo number, the struct's overall alignment according to the int size as to Zimo number, memory layout example is as follows

Memory layout for C + + objects

A C + + class contains class data member and class method members (class function member), and data members consist of static (static) data members and non-static data (nonstatic) members. Class member methods include static (static), non-static (nonstatic), and virtual (virtural)

The layout cost of C + + on the struct (class) and the cost of C language layout are different when virtural occurs.

That is, a C + + struct (class) and C struct with no virtural function,virtural base class have the same memory layout. C + + struct (class) inside the function does not appear in the class instance, constructor, destructor, operator overload as long as it does not contain virtural and only contains the basic data of the C struct is the same (here is a condition, C + + A static data member in a struct does not appear in the instance and does not take into account the case of nesting other structs, taking into account only the data member as the base data type.

typedef struct POINT_3D {         int x;         int y;         int z;} Structstruct point_3d{//c++ Language of the p;//c language structpublic:point_3d () {}point_3d (int a, int b, int C): X (a), Y (b), Z (C) {}~point_ 3d () {}void unit () {         x= x/sqrt (x*x+y*y+z*z);         y = y/sqrt (x*x+y*y+z*z);         z = z/sqrt (x*x+y*y+z*z);} Private:         int x;         int y;         int z;};


The two instances are the same on the memory layout!

Memory layout Rules for C + + objects

1. Non-static data members are configured within each object instance (class object), and static data members are stored outside of all object instances.

2. Static and non-crystalline method members are also stored outside of all object instances.

3. Virtual method virtural function is supported in two steps

3.1. Each class generates a bunch of pointers to virtual functions, stored in a table called virtual table (VTBL)

3.2. Each object instance is added with a pointer to the associated virtual table. This pointer is often called VPTR.

The Vptr setting setting and resetting resetting is done automatically by constructors, destructors, and assignment operators for each class.

The Type_info object associated with each class is also indicated by virtural table, usually placed in the first slot of the table.

Example

Class Point{public: Point   (float xval);   Virtual ~point ();   float x () const;   static int Pointcount ();p rotected:   virtual ostream& print (ostream &os) const;   float _x;   static int _point_count;};

Memory layout diagram (image from the Deep Exploration of C + + object model book)


About inheritance

If inheritance is taken into account, the problem will be much more complicated, and here is a brief description of the book, "explore the C + + object model in depth".

C + + can be single-inheritance, multi-inheritance, virtual inheritance (the base class in the inheritance chain regardless of how many times, will contain only one instance)

C + + was originally implemented as a direct inclusion of the parent class object, and later the implementation has some changes, some in addition to a bptr pointer to a parent object table, or some in the VTBL implementation, but must be in the data member section behind.

So

The part of the data member is consistent with the rules of C struct, so this part refers to the memory layout of C struct,

C + + structs without virtual methods end up with no vptr, and structs containing virtual methods add this.


Description

This article was published by Giantpoplar in Csdn

Article Address http://blog.csdn.net/giantpoplar/article/details/47658679

Reproduced please retain this note


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

Memory layout for C + + objects

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.