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

Source: Internet
Author: User

Struct/class/union memory alignment principles include:

1 ). data member alignment rules: data member of a structure (struct) (or union). The first data member is placed in a place where the offset is 0, in the future, the starting position of each data member should be stored from the size of the member or the size of the Member's sub-member (as long as the Member has sub-members, such as arrays and struct) (for example, if int Is 4 bytes on a 32-bit machine, it must be stored from the 4-byte integer). The basic types do not include struct/class/uinon.

2 ). struct as a member: If a structure contains some struct members, the struct members should be stored from an integer multiple of the "widest basic type member" in the structure. (struct a contains struct B, B contains char, int, double and other elements, and B should be stored from an integer multiple of 8 .).

3 ). final work: the total size of the struct, that is, the result of sizeof ,. it must be an integer multiple of the "widest basic type member" of its largest member. complete the deficiencies. (basic types do not include struct/class/uinon ).

4). sizeof (union), the maximum size element in the structure is the union size, because at a certain time point, union only has one member actually stored in this address.

 

Example: class is used as the example below.

No.1

class Data{    char c;    int a;}; cout << sizeof(Data) << endl;

No. 2

class Data{    char c;    double a;}; cout << sizeof(Data) << endl;

Obviously, the output result of Program No. 1 is 8 No. 2, and the output result is 16.

No.1 the largest data member is 4 bytes, 1 + 4 = 5, and the complement is a multiple of 4, that is, 8. While No. 2 is 8 bytes, 1 + 8 = 9, which is a multiple of 8, that 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 running result is 12 No. 4 running result is 8

When the data members in the class are put into the memory, the memory comes up with a memory block, and the data members queue up one by one to put in the memory. If the memory is too large, they don't split themselves into two halves, but how much they can put, instead, wait for the next memory block. In this way, we can understand why the output results of codes on both ends of No. 3 and No. 4 are different, because the left side is 1 + (3) + 4 + 1 + (3) = 12, the right side is 1 + 1 + (2) + 4 = 8. Bytes filled in brackets.

 

 

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;

Running results of No. 5 and No. 6 are: 48

By default, the memory alignment is based on the largest basic type in the class. If the class contains a custom type, the largest basic type is recursively used for comparison. In No. 5 and No. 6, the memory blocks are connected to the data members one by one. Until the first block is 5th, there is only one char in BigData and it is placed into the memory block, there are 7 bytes left in the memory block, followed by an int (4 bytes), which can be put down, so it also enters 5th memory blocks. At this time, there are 3 bytes left in the memory block, next is a double (8 bytes), which cannot be placed, so wait for the next memory to arrive. Therefore, the Data size of No. 5 is 33 + 4 + (3) + 8 = 48. Similarly, no. 6 should be 33 + (7) + 8 = 48.

 

By the way, Union indicates that several variables share the same memory location, saving different data types and variables of different lengths at different times. In union, all the common body members share one space and can only store the value of one of the member variables at a time.

 

 

 

 

 

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.