C language byte alignment problem __c language

Source: Internet
Author: User

One, what is aligned, and why to align:

1. The memory space in modern computers is divided by byte, theoretically, it seems that access to any type of variable can start at any address, but the reality is that access to a particular variable is often at a specific memory address, which requires that all types of data be arranged in space according to certain rules, Instead of sequencing one after another, that's the alignment.

2. The role of alignment and reasons: each hardware platform for storage space processing is very different. Some platforms have access to certain types of data only from certain addresses. Other platforms may not be able to do this, but the most common is the loss of access efficiency if the data is not aligned according to the requirements for its platform. For example, some platforms begin every time they are read from my even address, if an int (assumed to be 32 bits) is stored at the beginning of the even address, then a read cycle can be read, and if stored at the beginning of the odd address, it may take 2 reading cycles, and two read the results of the high and low bytes to get the int data. Obviously, the reading efficiency is much lower. This is also the game of space and time.

Second, the realization of alignment

Usually, when we write a program, we don't need to consider the alignment problem. The compiler chooses the alignment policy for the target platform for us. Of course, we can also notify the compiler to pass the precompiled instructions and change the alignment of the specified data.
However, because we don't generally need to care about this problem, because the editor is aligned with data storage, and we don't understand it, it's often confusing to some questions. The most common is the struct data structure of the sizeof results, unexpected. To do this, we need to understand the alignment algorithm.
Alignment algorithm:
Because of the different platforms and compilers, I use the GCC version 3.2.2 compiler (32-bit x86 platform) as an example to discuss how the compiler aligns the members of the struct data structure.
The structure body is defined as follows:
struct A {
int A;
Char b;
Short C;
};
The structure body A contains a 4-byte length int, a 1-byte length char and a 2-byte length of the short data one. So the space used by a is supposed to be 7 bytes. However, because the compiler wants to align data members in space.
So using the sizeof (Strcut A) value is 8.
Now adjust the order of the member variables to the structure.
struct B {
Char b;
int A;
Short C;
};
This is also a total of 7 bytes, but the value of sizeof (struct B) is 12.
Here we use the precompiled Instruction #pragma pack (value) to tell the compiler to replace the default by using our specified alignment value.
#progma Pack (2)//* Specify 2-byte alignment */
struct C {
Char b;
int A;
Short C;
};
#progma Pack ()/* To cancel the specified alignment and restore the default alignment */
The value of sizeof (struct C) is 8.

The modified alignment value is 1:
#progma Pack (1)//* Specify 1-byte alignment */
struct D {
Char b;
int A;
Short C;
};
#progma Pack ()/* To cancel the specified alignment and restore the default alignment */
The sizeof (struct D) value is 7.

For char data, its own alignment value is 1, for the short type 2, for the int,float,double type, its own alignment value is 4, Unit bytes.
There are four conceptual values in this:
1 The alignment value of the data type itself: the value of its own alignment for the underlying data type that is explained above.

2 Specifies the alignment value of the specified alignment value, #pragma pack (value).

3 The value of the structure or the self alignment of the class: The value that is the largest of its members.

4 valid alignment values for data members, structs, and classes: their own alignment values and the smaller value of the specified alignment value.

       with these values, we can easily discuss the members of specific data structures and their own alignment. Valid alignment value n is the final value used to determine how the data is stored in the address, most importantly. A valid alignment of n means "Snap to n", which means that the data "holds the starting address%n=0". Data variables in the structure are emitted in the order defined. The starting address of the first data variable is the starting address of the structure. The member variables of the structure should be aligned to emit, and the structure itself should be aligned according to its own valid value round (that is, the total length of the struct member variable should be an integral multiple of the effective alignment value of the structure, combined with the following example). This makes it easy to understand the values of the above examples.
Example Analysis:
Analysis example B;
struct B {
    char b;
    int A;
    s Hort C;
};
Assume that B starts emitting from the address space 0x0000. The specified alignment value is not defined in this example, which defaults to 4 in the author environment. The self alignment value of the first member variable B is 1, is smaller than the specified or specified alignment value of 4, so its valid alignment value is 1, so its address 0x0000 conforms to 0x0000%1=0. The second member variable A has its own alignment value of 4, so the valid alignment value is also 4, so it can only be stored in the four contiguous byte space from the starting address 0x0004 to the 0x0007, and the 0x0004%4=0 is reviewed, and the first variable is immediately located. The third variable, C, has its own alignment value of 2, so the valid alignment value is also 2, which can be stored in the two-byte space 0x0008 to 0x0009, in line with 0x0008%2=0. So everything from 0x0000 to 0x0009 is stored in B content. Then look at the data structure B's own alignment value for its variable maximum alignment value (here is B) so that is 4, so the effective alignment of the structure is also 4. According to the requirements of the structural body rounding, 0x0009 to 0x0000=10 Byte, (10+2)%4=0. So the 0x0000a to the 0x000b is also occupied by the structural body B. Therefore, B from 0x0000 to 0x000b a total of 12 bytes, sizeof (struct b) = 12;

Similarly, analyze the above example C:
#pragma pack (2)//* Specify 2-byte alignment */
struct C {
Char b;
int A;
Short C;
};
#pragma pack ()/* To cancel the specified alignment and restore the default alignment */
The first variable B has its own alignment value of 1, specifies that the alignment value is 2, so that its valid alignment value is 1, assuming C starts from 0x0000, then B is stored in 0x0000, conforms to 0x0000%1= 0, the second variable, its own alignment value is 4, the specified alignment value is 2, so the valid alignment value is 2, So the order is stored in 0x0002, 0x0003, 0x0004, 0x0005 four consecutive bytes, in line with the 0x0002%2=0. The third variable C has its own alignment value of 2, so the valid alignment value is 2, in order to store
In 0x0006, 0x0007, in line with 0x0006%2=0. So from 0x0000 to 0x00007 a total of eight bytes is stored in C variables. and C's own alignment value is 4, so C's valid alignment value is 2. Also 8%2=0,c occupies only eight bytes of 0x0000 to 0x0007. So sizeof (struct C) =8.

With the above explanation, I believe you should have a clear understanding of the byte alignment concept of C language. In web programs, it's important to master this concept. To pass a 2-stream (such as a struct) between platforms (such as between Windows and Linux), you must define the same alignment between these two platforms, or else somehow some mistakes are made, but it's hard to ^_^.

Related Article

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.