C ++ memory Alignment Technology Learning

Source: Internet
Author: User

I learned the memory Alignment Technology in the C ++ struct today, so I searched for the memory Alignment Technology in the evening.

I. Reasons for memory alignment most references are as follows:

1. Platform reason (reason for transplantation): Not all hardware platforms can access any data on any address. Some hardware platforms can only retrieve certain types of data at some addresses, otherwise, a hardware exception is thrown.
2. Performance reasons: Data Structures (especially stacks) should be aligned on natural boundaries as much as possible. The reason is that in order to access non-alignment memory, the processor needs to perform two memory accesses, while alignment memory access only needs one access.

Ii. Alignment rules each compiler on a specific platform has its own default alignment coefficient (also called alignment modulus ). Programmers can use the pre-compiled command # pragma pack (n), n =, and 16 to change this coefficient. n is the alignment coefficient you want to specify ".

Rules:
1. Data member alignment rules: the data member of the structure (struct) (or union). The first data member is placed at the position where the offset is 0, in the future, the alignment of each data member is performed according to the value specified by # pragma pack and the smaller value of the data member's length.
2. Overall alignment rules of the structure (or union): After the data members align themselves, the structure (or union) itself also needs to be aligned, alignment is performed according to the value specified by # pragma pack and the smaller value in the maximum data member length of the structure (or union.
3. When the n value of # pragma pack is equal to or greater than the length of all data members, the n value does not have any effect. The union C ++ shared body statement is as follows:
Union shared body name
{Data type member name;
Data Type member name;
...} Variable name;
Struct C ++ struct [template-spec] struct [ms-decl-spec] [tag [: base-list] {
Member-list}
[Declarators];
[Struct] tag declarators;
Union UA {int a [3];
// 20 short B;
// 2 double c;
// 8 char p2;
// 1 };
Union UA1 {
Int B;
// 2 double c;
// 8 short p2;
// 1 };
Typedef struct UB1 {int I;
// 4 double x;
// 8 short j;
// 4 };
Struct UB {int n;
// 4-byte UA;
// 24-byte char c [10];
// 10 bytes };
Class
{Public: (){};
~ A (){};
UB1 B;
Char * p;
};
Int main ()
{Std: cout <"UB1 size" <

 


From swrd

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.