5 minutes to fix memory byte alignment

Source: Internet
Author: User
Tags pack

Original address: http://blog.csdn.net/hairetz/article/details/4084088

--------------

Write a struct, and then sizeof, will you often be surprised by the results? sizeof's results tend to be bigger than the total length of the variable you're declaring, what's going on? Tell me about byte alignment.

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

If the architecture is misaligned, the members in a will be stored one after another, so sizeof (a) is 11. Obviously, the 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 process for this architecture is to handle the data with a width of W. It is also designed to take precedence over the efficiency of W-bit data operations. such as reading and writing ..... 500,000 words omitted here

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

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

Update: Why byte alignment

1: Data member Alignment rule: 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, structure, etc.), starting with an integer multiple (such as an int at a 32-bit machine of 4 bytes, starting with an integer-multiple address of 4.

2: struct as a member: if there are some struct members in a struct, the struct member is stored from the integer multiple address of its internal maximum element size. (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: The finishing touches: the total size of the structure, which is the result of sizeof. Must be an integer multiple of its internal maximum member. Insufficient to be filled.

When you finish reading this 3 principles, 2 minutes has passed, seize the time, the actual combat 3 minutes:

typedef struct BB
{
int id; [0] .... [3]
Double weight; [8] ..... [15] Principle 1
float height; [16]. [19], the total length to 8 of the integer 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, above the full look understand, memory alignment basic clearance.

Tell me more about #pragma pack ().

Add a #pragma pack (1) in front of the code, and you'll be happy to find that the above code output is

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

This is not an ideal world without memory alignment. Yes, #pragma pack (1), tell the compiler that all alignments are aligned in integers of 1, in other words there are no alignment rules.

Do you understand me?

What about the results of the #pragma pack (2)? Sorry, 5 minutes to go, test it yourself.

Ps:vc,vs such as compiler default is #pragma pack (8), so test our rules will be normal; note that GCC is #pragma pack (4) By default, and GCC only supports 1,2,4 alignment. The alignment value calculated in the three principles cannot be 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.