Computer memory address alignment and class size

Source: Internet
Author: User

Alignment in computer memory: from Baidu Encyclopedia and blog: http://m.blog.csdn.net/article/details?id=44587587One, what is aligned, and why to align⒈ Modern computer memory space is divided by byte, theoretically, it seems that access to any type of variable can start at any address, but the reality is that access to a particular variable is often at a specific memory address, which requires that all types of data be arranged in space according to certain rules, Instead of sequencing one after another, that's the alignment.The role and reason of ⒉ alignment:Each hardware platform has a great difference in the processing of storage space. Some platforms have access to certain types of data only from certain addresses. Other platforms may not be able to do this, but the most common is the loss of access efficiency if the data is not aligned according to the requirements for its platform. For example, some platforms begin every time they are read from my even address, if an int (assumed to be 32 bits) is stored at the beginning of the even address, then a read cycle can be read, and if stored at the beginning of the odd address, it may take 2 reading cycles, and two read the results of the high and low bytes to get the int data. Obviously, the reading efficiency is much lower. This is also the game of space and time. Second, the realization of alignment Usually, when we write a program, we don't need to consider the alignment problem. The compiler chooses the alignment policy for the target platform for us. Of course, we can also notify the compiler to pass the precompiled instructions and change the alignment of the specified data. However, because we don't generally need to care about this problem, because the editor is aligned with data storage, and we don't understand it, it's often confusing to some questions. The most common is the struct data structure of the sizeof results, unexpected. To do this, we need to understand the alignment algorithm. Alignment algorithm: Due to the different platforms and compilers, I use the GCC version 3.2.2 compiler (32-bit x86 platform) as an example to discuss how the compiler for the struct data structure of the members to align. The structure body is defined as follows: struct a {int A; char b; short C;}; The structure body A contains a 4-byte length int, a 1-byte length char and a 2-byte length of the short data one. So the space used by a is supposed to be 7 bytes. However, because the compiler wants to align data members in space. So using the sizeof (Strcut A) value is 8. Now adjust the order of the member variables to the structure. struct B {char b; int A; short C;}; This is also a total of 7 bytes, but the value of sizeof (struct B) is 12. Here we use the precompiled Instruction #pragma pack (value) to tell the compiler to replace the default by using our specified alignment value. #progma pack⑵/* Specifies 2-byte alignment */struct C {char b; int A; short C;}; #progma Pack ()/* Cancels the specified alignment and restores the default alignment/sizeof (struct C) value of 8. The modified alignment value is 1: #progma pack⑴/* specifies 1-byte alignment */struct D {char b; int A; short C;}; #progma Pack ()/* Cancels the specified alignment and restores the default alignment/sizeof (struct D) value of 7.

Example 1:

struct struct1{

Char A1;

Char B1;

};

the size of the structure body Struct1 is 2 bytes.
For char data, its own alignment value is 1, for the short type 2, for the int,float,double type, its own alignment value is 4, Unit bytes. There are four conceptual values in this:1 The alignment value of the data type itself: the value of its own alignment for the underlying data type that is explained above. 2 Specifies the alignment value: #pragma pack (value) to specify the alignment value. (if the specified is not displayed, align to the largest member itself) 3 The value of the structure or the self alignment of the class: The value that is the largest of its members. 4 Valid alignment values for data members, structs, and classes: their own alignment values and the smaller value of the specified alignment value. With these values, we can easily discuss the members of the specific data structures and their own alignment. Valid alignment value n is the final value used to determine how the data is stored in the address, most importantly. A valid alignment of n means "Snap to n", which means that the data "holds the starting address%n=0". Data variables in the structure are emitted in the order defined. The starting address of the first data variable is the starting address of the structure. The member variables of the structure should be aligned to emit, and the structure itself should be aligned according to its own valid value round (that is, the total length of the struct member variable should be an integral multiple of the effective alignment value of the structure, combined with the following example). This makes it easy to understand the values of the above examples. Example Analysis: analysis example B; struct B {char b; int A; short C;}; Let's say B starts emitting from the address space 0x0000. The specified alignment value is not defined in this example, which defaults to 4 in the author environment. The self alignment value of the first member variable B is 1, is smaller than the specified or specified alignment value of 4, so its valid alignment value is 1, so its address 0x0000 conforms to 0x0000%1=0. The second member variable A has its own alignment value of 4, so the valid alignment value is also 4, so it can only be stored in the four contiguous byte space from the starting address 0x0004 to the 0x0007, and the 0x0004%4=0 is reviewed, and the first variable is immediately located. The third variable, C, has its own alignment value of 2, so the valid alignment value is also 2, which can be stored in the two-byte space 0x0008 to 0x0009, in line with 0x0008%2=0. So everything from 0x0000 to 0x0009 is stored in B content. Then look at the data structure B's own alignment value for its variable maximum alignment value (here is B) so that is 4, so the effective alignment of the structure is also 4. According to the requirements of the structural body rounding, 0x0009 to 0x0000=10 Byte, (10+2)%4=0. So the 0x0000a to the 0x000b is also occupied by the structural body B. So B has a total of 12 bytes from 0x0000 to 0x000b, sizeof (struct b) = 12.   C + + class storage space The above examples are about the structure of the stored calculation, in fact, the class memory calculation method is the same, C + + objects in the storage range of the main non static data members occupy memory, member functions are not occupied in memory class object bytes.   Note that the class itself does not occupy memory, and the instantiated object consumes memory, but can use sizeof (class name/object name) to view memory size. Why just include a non-static data object. Because the static data does not belong to any object of the class, it is the property of the class, not the property of the specific object, and the entireOnly one memory region in the storage area stores the corresponding static data, which means that all class objects share the data, so it is not possible to calculate the memory space of a specific object or type. (We know that the static data is not an object attribute, but a property of the class, and that he cannot be considered an object or type of storage, and can only declare in the class definition that initialization can only be performed outside of the class)  Note: A derived class occupies a memory size that is independent of the way it is inherited, and may only be inaccessible. When there is a virtual function in a class: In a Class object storage space, the first position requires 4 bytes to store a pointer. This pointer is a virtual function table that points to a modified class. The value of this pointer is the address of the virtual function table for the class. So it's 4 bytes more than that.

For example:

Class D

{

Public

virtual void F () {}; 4

Double D; Double has its own alignment of 8, effectively aligning min (8,4) = 4,

4+8=12, according to the requirements of the structural body rounding. if the specified alignment value is not displayed, the maximum member itself is aligned (double 8), so the valid alignment of the structure is 8

D D;

sizeof (d) = 16; ///... Why is it

Derived class memory size

For example:

Class E:d

{

int D0;

char c;

int D1;

};

e e;

sizeof (e) = 32;

Explain:

A virtual function is in the base class, so the derived class object starts with 4 bytes to store a pointer to the virtual function table. (even though subclasses have new virtual functions, there is only one virtual function table in the instance, and a 4-byte pointer to the virtual function table is stored.) The newly added virtual function address is appended to the inherited virtual function table. To see my virtual function inheritance

Then inherit the data member in D, double D;

It wants 8-byte alignment, so the front space is 4 bytes.

The storage d0,c,d1 is started below. The last class alignment can be computed by 32.

Therefore, you can think of the size of the storage space of the derived class object:
base class storage space + storage space for non-static data members peculiar to derived classes
You can no longer add storage space for virtual functions in an inheriting class (because all virtual functions share a single area of memory) and only need to consider the amount of memory space that the derived class center adds to the non-static data members.
The vptr is stored at the beginning of the memory, and the subclass inherits the parent memory first and then its own new memory.


Adjusting the layout of struct member variables is one way to reduce the number of memory accesses. Here are two different types of knots


The structure data member adjusts the scheme, all can obtain the very good performance enhancement.


1. The size of the structure is reduced by the size of the member's memory alignment in ascending or descending order. Look at the following two structures


Body:

struct beforeadjust{

Char A;

Short B;

int C;

Char D;

}; sizeof () =12

struct afteradjust{

Char A;

Char D;

Short B;

int C;

}; sizeof () =8


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.