"Go" C + + struct/class/union memory alignment

Source: Internet
Author: User

Original link: http://www.cnblogs.com/Miranda-lym/p/5197805.html

There are four struct/class/union memory alignment principles:

1). Data member Alignment rules: A data member of a struct (struct) (or union), where the first data member is placed at offset 0, where the starting position of each data member is stored from the member size or the member's Child member size (as long as the member has child members, such as an array , struct, etc.) begins with an integer multiple (for example, an int is 4 bytes in a 32-bit machine, and is stored from an integer-multiple address of 4), and the base type does not include Struct/class/uinon.

2). Struct as a member: if there is some struct member in a struct, the struct member is to be stored from the integer multiple address of its internal "widest base type member". (struct A has a struct b,b with char,int, double and so on, and that B should be stored from an integer multiple of 8.)

3). Closing work: The total size of the structure, which is the result of sizeof. Must be an integer multiple of the "widest base type member" of its internal maximum member. Insufficient to be filled. (the base type does not include Struct/class/uinon).

4). sizeof (Union), the size of the largest element of the structure inside the size of the union, because at some point, only one member of the union is actually stored at that address.

Example explanation: The following class as the Representative

The

Class data{    char c;    int A;}; cout << sizeof (Data) << Endl;

No.2

Class data{    char c;    Double A;}; cout << sizeof (Data) << Endl;

Obviously the result of the output of the program is 8 no.2 output is 16.

The largest data member is 4bytes,1+4=5, which is a multiple of 4, which is 8. The No.2 is 8bytes,1+8=9, which is a multiple of 8, which is 16.

No.3

Class data{    char c;    int A;    char D;}; cout << sizeof (Data) << Endl;

No.4

Class data{    char c;    Char D;    int A;}; cout << sizeof (Data) << Endl;


No.3 Run result is 12

No.4 Run result is 8

When the data members in the class are put into memory, the memory takes out a block of memory, and the data members queue up one by one, encountering too much, not splitting themselves in half, how much to put, but waiting for the next memory block to come over. In this case, you can understand why the code output on both ends of the no.3,no.4 is different, because the left side is 1+ (3) +4+1+ (3) = 12, and the right side is 1+1+ (2) +4=8. The bytes in parentheses.

No.5

Class bigdata{    char array[33];}; class data{    bigdata BD;    int integer;    Double D;}; cout << sizeof (bigdata) << "   " << sizeof (Data) << Endl;

No.6

Class bigdata{    char array[33];}; class data{    bigdata BD;    Double D;}; cout << sizeof (bigdata) << "   " << sizeof (Data) << Endl;

No.5 and No.6 operation results are: 48

By default, memory alignment is based on the largest base type in class, and if there is a custom type in class, recursively takes the largest base type to participate in the comparison. In No.5 and No.6 memory block one after another to pick up the data members, until the 5th block, the Bigdata only 1 char, put it into memory block, memory block also left 7 bytes, followed by an int (4bytes), can be put down, so also into the 5th memory block, this time the memory block remains 3bytes, and then a double (8bytes), not fit, so wait for the next memory to come. Therefore, the No.5 data of size=33+4+ (3) +8=48, the same No.6 should be 33+ (7) +8=48.

By the way union: a common body means that several variables share a single memory location, saving different data types and variables of varying lengths at different times. In union, all the members of the common body share a space and only the value of one of the member variables can be stored at the same time.

The above content is reproduced in part from http://www.cnblogs.com/biyeymyhjob/archive/2012/09/07/2674992.html

Expand:

Number of bytes for various data types under a 32-bit operating system

1. Integral type

int 4 bytes

Long int 4 bytes

Short int 2 bytes

unsigned long int 4 bytes

unsigned short int 2 bytes

2, character type

Char 1 bytes

unsigned char 1 bytes

3, floating-point type

Float 4 bytes

Double 8 bytes

Long double 8 bytes

unsigned long double 8 bytes

Unsigned double 4 bytes

4. String type

String 32 bytes

5. Pointer type

All types of pointers are 4 bytes (but 8 bytes under 64 bits)

Two, byte alignment principle

Under System default alignment: The offset of each member relative to the address of the struct variable is exactly the integer multiple of the byte that the member type occupies, and the final consumption of bytes is an integer multiple of the maximum number of bytes in the member type.

Third, thedata members in the C + + class also follow the above alignment principle, namely:

1. The sizeof (custom class) is evaluated in a similar manner, regardless of (or without) virtual functions and virtual inheritance.

2. However, if a class has virtual functions or virtual inheritance, it is equivalent to a data member of one pointer type (position in front of all data members) on the basis of the data member, plus the last calculation.

3. Static (passive) members are not in the computed range.

[In short, the size of a class is related to member variables (non-static data member variables) and virtual function pointers (note: A virtual function has a pointer to a list of virtual functions, regardless of how many virtual functions occupy a single byte size)]

4. If a class or struct does not contain any data members, and there is no virtual function and virtual inheritance, the sizeof () structure is 1.

Four, Why byte alignment?

In Windows core programming, this says that when the CPU accesses the properly aligned data, it runs most efficiently , and the data is aligned when the memory address of the data-size data module is 0 o'clock. For example, the word value should always start at the address that was removed by 2, and the DWORD value should always start at the address that was removed by 4, and the data alignment is not part of the memory structure, but rather part of the CPU structure. When the CPU tries to read a value that is not aligned correctly, the CPU can do one of two things: an exception condition is performed, and multiple aligned memory accesses are executed to read the complete unaligned data, and if memory access is performed more than once, the application runs slower.

Refer to Webary's blog:http://www.cnblogs.com/webary/p/4721017.html

"Go" C + + struct/class/union memory alignment

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.