Deep parsing C + + Data member memory layout _c language

Source: Internet
Author: User
Tags inheritance

If a class has only a class name defined, no methods and fields are defined, such as class a{}; Each instance of class A consumes 1 bytes of memory, and the compiler will insert a char in this instance to ensure that each instance of a has a unique address in memory, such as a a,b;&a!= &b. If a direct or indirect inheritance (not virtual inheritance) of multiple classes, if this class and its parent class have no method without a field like a, then the size of each instance of the class is 1 bytes, and if there is a virtual inheritance, it is not 1 bytes, each virtual inherits a class, An instance of this class will have one more pointer to the virtual inheritance of the parent class. It is also worth noting that a class like a, the compiler does not necessarily produce the legendary 6 methods, which will only be generated when needed, such as Class A, which is not used anywhere. These method compilers do not have to be generated, and if this class is instantiated, then default is generated Constructor, while destructor is not necessarily produced.

If there is a static data member,nonstatic datamember in a class, and a const data Member,enum, what is its memory layout like? Look at the simple class point below:

Copy Code code 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 12 bytes, I believe that a lot of people know which of the member variables are occupied, that is, x,y,maxcount,maxcount as a constant field, but there may be different values in each instance of point. Of course, part of the point instance, if the maxcount is defined as static, then it is not a part of the point instance, if defined as a static const int maxcount=1; The maxcount is allocated in the. Data segment and, if not initialized, is allocated in the. BSS segment, regardless of the instance of point, where count is allocated in the. BSS segment, and Mincount is assigned to the. Rdata segment, count,maxcount,mincount after the compilation connection is complete , the memory (virtual address) is allocated, and when the program loads, it will have their virtual address corresponding to the actual physical address.

Data member's memory layout: nonstatic data member's order in class object and its stated order, static data member and Const member are not class Because they have only one copy and are shared by class object, static data member and const data member do not respond to the size of class object. The information about the paragraph, I think is each C/s + + programmer must know. Each time the point is instantiated, it only needs to allocate the memory that X,y,maxcount needs.

The data member of each class should be contiguous in memory, and in the event of alignment, there may be gaps in the middle. take a look at the following classes:

Copy Code code as follows:

Class AA
{
Protected
int X;
Char A;
};

Class Bb:public AA
{
Protected
Char b;
};

Class Cc:public BB
{
Protected
char c;
};


Sizeof (AA) =8//aligned to 3 bytes
Sizeof (BB) =12//two 3-byte alignment
Sizeof (CC) =16//compiler "Shameless" with 3 3-byte alignment



Why should the compiler shamelessly add 3 3-byte alignments to the class cc so that each instance of the CC is 9 bytes large. If the compiler does not add these 9-byte whitespace, then each instance of CC is 8 bytes, the previous x is 4 bytes, the following a,b,c account for 3 bytes, plus 1 bytes of white space alignment, just 8 bytes, no one is silly very naïve thought the best is to occupy 7 bytes bar.

If the CC consumes 8 bytes of memory, the same AA,BB is 8 bytes of memory, in this case, if a pointer to a AA instance is assigned to a pointer to a CC instance, then the 8 bytes in AA are covered directly to 8 bytes of CC, and the b,c in the CC instance is assigned the value that is not what we want. , which is likely to cause problems with your program.

The data member of the parent class has a full copy in the instance of the subclass, so that the type conversion between classes that have an inheritance relationship is simply the point where the pointer is modified.

Access to Data member. For access to 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 between pointers to object or pointer to data member, minus one to data member. The offsets for each data member are known at compile time, depending on the type and memory alignment of the members variable, there are virtual inheritance or virtual methods The compiler automatically adds some auxiliary pointers, such as pointers to virtual methods, pointers to virtual inheritance, and so on.

In the case of data member's access efficiency, struct member, class member, single inheritance, or multiple inheritance are equally efficient, because their storage is actually &obj+ (&class.datamember-1). In the case of virtual inheritance, may affect storage performance, such as accessing a data member with a pointer to a virtual inheritance, then performance can be affected, because at the time of virtual inheritance, it is not possible to determine whether this data is from a subclass or a parent class. Can be inferred only when it is run, in fact, is more than one step of the operation of the pointer, in virtual inheritance, if it is through the object instance to manipulate the virtual inheritance of data member, there will be no performance problems, because there is no polymorphism, everything in the compilation of memory address is determined.

Virtual inheritance or virtual methods in order to achieve polymorphism, a step, if not the need for polymorphism, but through the object instance to invoke the relevant methods will not have a performance problem.

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.