I'll teach you 5 minutes. Memory byte Alignment _c language

Source: Internet
Author: User
Tags pack

Write a struct, and then sizeof, will you often be surprised by the results? sizeof's results are often greater than the total length of the variables you declare, what's going on? Say byte alignment.

/****************************** Split Line

If the architecture is misaligned, the members of a will be stored one by one, thus sizeof (a) is 11. It is clear that alignment is a waste of space. So why use alignment?

The alignment and misalignment of the architecture is a trade-off between time and space. The alignment saves time. Assuming that the word length of an architecture is W, it also assumes that the most frequent and important processing of data with a width of W is in this architecture. Its design is also from the priority to improve the operation of W-bit data to consider the efficiency. For example, when reading and writing ... 500,000 words omitted here

*******************************************************/

The above is you casually Google, others can explain to you, a lot of truth, we don't have much time to discuss why we should align. straight into the topic, how to determine the memory alignment rules, sizeof results, please keep in mind the following 3 principles: (In the absence of #pragma pack macros , be sure to read the last line)

1: data member Alignment rule: The data member of the struct (struct) (or union), where the first data member is placed at the offset of 0, The starting position of each data member store in the future is from the size of the member or the member's child member size, as long as the member has child members, for example, an array, structure, etc.) of the integer multiple start (for example, int in the 32-bit machine is 4 bytes, you want to start from 4 of the integer multiple address to store.

2: A struct is a member: if a struct has certain members of the structure, the member of the struct body is to be stored from the integer multiple address of its internal maximum element size. (in struct a There are char,int, double, etc. in struct b,b, which should be stored as an integral number of 8.)

3: Finishing work: The total size of the structure, that is, the result of the sizeof,. Must be an integer multiple of the largest member within it. The insufficiency must be filled.

After you read this 3 principles, 2 minutes have passed, seize the time, combat 3 minutes:

Copy Code code as follows:

typedef struct BB
{
int id; [0] ..... [3]
Double weight; [8] ... [15] Principle 1
float height; [16]. [19], total length to be 8 integral times, padded [20] ... [23] Principle 3
}BB;

typedef struct AA
{
Char name[2]; [0],[1]
int id; [4]. [7] Principle 1

Double score; [8] ..... [15]
Short grade; [16],[17]
BB b; [24] ... [47] Principle 2
}AA;

int main ()
{
AA A;
Cout<<sizeof (a) << "" <<sizeof (BB) <<endl;
return 0;
}


The result is

48 24
OK, the above all see understand, memory alignment basic clearance.

Tell me more about #pragma pack ().

Add a #pragma pack (1) before the code, and you'll be glad to see that the code above is output to

32 16
BB is 4+8+4=16,aa is 2+4+8+2+16=32;

Is this not the ideal world with no memory alignment? Yes, #pragma pack (1) tells the compiler that all alignments are aligned according to the integer multiples of 1, in other words, there is no alignment rule.

Do you understand me?

What about the results of the #pragma pack (2)? I'm sorry, it's 5 minutes.

The Ps:vc,vs compiler defaults to the #pragma pack (8), so testing our rules is normal; note that GCC defaults to #pragma pack (4), and GCC only supports 1,2,4 alignment. The alignment value computed in the three principles is not greater than the n value specified by the #pragma pack.

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.