In-depth analysis of C ++ Data Member memory Layout

Source: Internet
Author: User

If A class only defines the class name and does not define any methods or fields, such as class A {}; then each instance of class A occupies 1 byte of memory, the compiler inserts A char in this instance to ensure that each A instance has a unique address in the memory, such as a A, B; &! = & B. If A direct or indirect inheritance (not A virtual inheritance) contains multiple classes, if the class and its parent class have no method and no field like, the size of each instance of this class is 1 byte. If there is virtual inheritance, it is not 1 byte, and each virtual inherits a class, the instance of this class will have a pointer pointing to the parent class of the virtual inheritance. Another point worth noting is that for classes like A, the compiler may not necessarily generate the six legendary methods that will only be generated when needed, for example, if class A is not used anywhere, the compiler of these methods is unnecessary. If the class is instantiated, default constructor is generated, whereas destructor is not necessarily generated.

If a class includes static data member, nonstatic data member, const data member, and enum, what is its memory layout,Let's look at the simple class Point below:

Copy codeThe Code is as follows:
Class Point
{
Public:
Point (): maxCount (10 ){}
Private:
Int X;
Static int count;
Int Y;
Const int maxCount;
Enum {
MinCount = 2
};
};


Sizeof (Point) = 12. Why is it 12 bytes? I believe many people know which member variables are occupied by X, Y, maxCount, and maxCount as constant fields, however, each instance of a Point may have different values. Of course, it is part of a Point instance. If maxCount is defined as static, it is not a part of a Point instance, if it is defined as static const int maxCount = 1, maxCount is allocated in. the data segment is allocated in. in the bss segment, it is irrelevant to the Point instance and the count is allocated in. in the bss segment, minCount is allocated in. in the rdata segment, after the total count, maxCount, and minCount are compiled and connected, the memory (Virtual Address) is allocated. When the program is loaded, their virtual addresses are mapped to the actual physical addresses.

Memory layout of Data member: the order of nonstatic data member in class object is the same as that in its declarative order. static data member and const member are not in class object because they only have one copy, shared by class objects, so static data member and const data member do not respond to class object size during enumeration. I think every C/C ++ programmer must know the section information. The Point instance only needs to allocate the memory required by X, Y, and maxCount.

Data member of each class should be continuous in the memory. If data Alignment occurs, there may be blank areas in the middle.See the following classes:

Copy codeThe Code is as follows:
Class AA
{
Protected:
Int X;
Char;
};

Class BB: public AA
{
Protected:
Char B;
};

Class CC: public BB
{
Protected:
Char c;
};


Sizeof (AA) = 8 // alignment 3 bytes
Sizeof (BB) = 12 // two 3-byte alignment
Sizeof (CC) = 16 // the compiler uses three 3-byte alignment shamelessly



Why is it shameless to add three 3-byte alignment in class CC, so that each CC instance will be 9 bytes larger. If the compiler does not add the blank nine bytes, each instance of CC is 8 bytes. The preceding X occupies 4 bytes, and the following a, B, and c occupy 3 bytes, add the blank alignment of 1 byte, Which is exactly 8 bytes. No one is so naive that it is best to take 7 bytes.

If CC occupies 8 bytes of memory, the same AA and BB are all 8 bytes of memory, in this case, if you assign a pointer to the AA instance to a pointer to the CC instance, then, the 8 bytes in AA will be directly covered to the 8 bytes of CC. As a result, B and c in the CC instance are assigned with a value that is not what we want, this may cause problems in your program.

The data member of the parent class has a complete copy in the subclass instance. In this way, type conversion is performed between classes with inheritance relationships, and the pointer pointing is simply modified.

Data Member access. To access a data member, the compiler adds the starting address of the object instance to the offset of data member. Such as CC c;

C. X = 1; equivalent to & c + (& CC: X-1), minus one is actually to distinguish whether it is to object pointer or to data member pointer, point to data member by one. The offset of each data member is known during compilation. Based on the type and memory alignment of the member variables, the compiler automatically adds some auxiliary pointers when virtual inheritance or virtual methods exist, for example, a pointer to a virtual method or a pointer to a virtual inherited parent class.

In terms of data member access efficiency, the efficiency of struct member, class member, single inheritance or multi-inheritance is the same, because their storage is actually all & obj + (& class. datamember-1 ). In the case of virtual inheritance, the storage performance may be affected. For example, if you use a pointer to access a data member that is inherited by virtual inheritance, the performance may be affected because in the case of virtual inheritance, during compilation, it cannot be determined whether the data member is from a subclass or parent class. It can be inferred only when it is running. In fact, it is a one-step pointer operation. In virtual inheritance, if you operate the data member inherited by virtual machines through an object instance, there will be no performance problems, because there is no polymorphism, and the memory address of everything is determined during compilation.

The virtual inheritance or virtual method is the same as the implementation of polymorphism. If there is no need for polymorphism, but the relevant method is called through the object instance, there will be no performance problems.

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.