Natural alignment of struct (Classic)

Source: Internet
Author: User

Natural alignment of struct
Institution body below
Struct
{
Char C;
Int I;
Char cc;
};
What is its sizeof? If it is compact alignment, of course it is sizeof (char) + sizeof (INT) + sizeof (char); but the compiler does not compact alignment by default, on a 32-bit machine, the compilation result is 12. This is a char that occupies an int space. In other words, there are three idle bytes between C and I! This is the effect of natural alignment.
Why natural alignment?
When I first talked about Baidu's Gg e-face, I explained it in a random way. I really admire my nonsense ability. In the original X86 architecture, the IP address needs to be aligned during CPU access. For example,When the short type is used, the address must be a multiple of 2. If the int type is used, the address must be a multiple of 4. If not, the CPU access efficiency may be reduced.For example, if the IP address of an int is 0x03, the first byte of the int is obtained based on the IP address 0x00, and the remaining three byte is obtained based on the IP address 0x04. Therefore, the original memory access is done, but twice,Just put off your pants and fart.Haha.
How to calculate the naturally aligned sizeof?
This is the interviewer's killer, haha.
It is actually a rule --It depends on the length of the CPU..
On a 32-bit CPU, the alignment unit is 4 bytes. I have not tried 64bit. Who has an environment.
Example:
Struct B
{
Char CA;
Char CB;
Char CC [10]; // This char + char array is connected and aligned by char
Int I;
Char CD [3]; // a byte is empty.
};
Sizeof () is 20; note that continuous char is always aligned by char. the char in the excess part is aligned by Int. Be careful with you, we may find that the header files in the network header on Linux, such as the ip tcp, are all defined using Char or char arrays, thus avoiding the natural alignment problem, yes, it is always compact aligned according to Char, enhancing program portability. I have made this mistake. When I parse the network header, int and char appear in struct at the same time. The result of natural alignment is that the ARP Protocol cannot be correctly parsed, I found this bug only half a year after being depressed. In fact, this is not a bug. It is a linguistic feature (derived from expert C).
Some other friends on the network said that they were alignment according to the largest unit in the struct, which is wrong because the case he gave was the largest in int, the 32-bit CPU is always aligned by 32-bit, which is exactly the length of the int, which leads to a wrong understanding.
Example
Struct C
{
Double D;
Char C;
};
The size of sizeof () is 12, not 2 * sizeof (double.
Struct d
{
Struct B;
Struct C;
};
The size of sizeof () is the size of sizeof (B) + sizeof (c), not 2 * sizeof (B;
How to make the structure compact and align?
GCC provides an option-fpack-struct, but the Compact alignment of the compilation, other platforms can be done with # pragma pack, see the page http://www.mscenter.edu.cn/blog/mingge/archive/2005/12/10/7655.html

 

Bytes ----------------------------------------------------------------------------------------------------------------------

The struct is aligned in units of the largest internal basic data type and smaller values in the compiler's byte alignment settings.

For example, struct a {int I; char C ;}; is aligned with int type (4 bytes), so the entire struct size is 8.

Struct B {char K; char C ;}; alignment with char type (1 byte), so the entire structure size is 2.

The basic data type is aligned with the smaller value in this type length and compiler byte alignment setting.

In VC # pragma pack (1), set the byte to 1-byte alignment. The default value is byte alignment. It can only be set to the power of 2.

For example, the size of struct c {char K; int I ;}; is 8, and the I address starts from the last three bytes of K, because int is 4-byte aligned.

Another example is: # pragma pack (1) struct d {char K; int I ;};# Pragma pack () // restore; the size is 5, because int is 1-byte aligned. The entire struct is also changed to 1-byte alignment by the alignment of the int type.

 

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

MIPS architecture

Recently, more and more people have asked me questions about mips architecture. Here we will summarize them.
Generally, you can find answers to questions about the mips architecture at the level of the profession.

1.
Q: How many general-purpose registers does MIPS have?
A: 32.

2.
Q: When I was reading the disassembly code, I saw some registers named Zero, A1, a2... and SP, Ra. Why give them these names?
A: According to the general conventions of the compiler, some registers are used for special purposes. For example, SP is the stack pointer and Ra is the return address of function calls. Of course, you may not program according to these Conventions, such as using $2 to store the stack pointer and $3 to store the return address, but such a program cannot work with the standard library link.

3.
Q: Why is the SP register of MIPS also a general register? What's more, MIPS does not seem to have a dedicated Command for pushing stacks/issuing stacks.
A: This is one of the major differences between MIPS and x86 CISC processors. There is no dedicated hardware-implemented stack register/instruction. Instead, it is implemented by software. In the function entry, the stack pointer decreases, the stack grows to a low address, the stack value is restored at the return, and the stack frame is destroyed.

4.
Q: If the MIPs stack is implemented by software, will the function call overhead of MIPS increase?
A. However, we actually know that MIPs uses four GPR (general purpose register) to pass the first four word function parameters, while x86 only has one eax register to pass function parameters. Therefore, if the function parameter is smaller than or equal to four words, you do not need to use the stack. We know that most function parameters are within four words. -- Therefore, this is not too worried. In addition, when writing a function with too many parameters, we recommend that you use struct to pass these parameters.

5.
Q: The MIPs LH and LB commands will change the first two/1 bytes in the register or the last two/1 bytes?
A: Generally, it is low.

6.
Q: Is MIPS big-Endian or little-Endian )?
A: This is configurable. Generally, we use the big-end mode, because almost all network protocols and data fields in data packets are encapsulated in the big-end mode.

7.
Q: I have seen in news reports that the "Dragon core" and MIPS commands 95% are the same. So what is different?
A: The four "Non-Alignment loading/storage" commands of MIPs are protected by the patent law. godson cannot support these four commands without authorization.

8.
Q: What are non-alignment loading/storage commands? Under what circumstances will these commands be used?
A: MIPS memory access requires byte alignment. For example, the 4-byte access Command requires that the access start address be divisible by 4, for example, 0x8100223c. If you access the address 0x8100223d, an exception will occur ). When processing this exception, the MIPs processor splits the load into two non-aligned loads.

9.
Q: Why is there no such restriction on x86?
A: In fact, x86 also has this problem. Non-Alignment access will be split into two executions. But all this is done by hardware and transparent to the code level-in this way, a programmer who does not understand this principle can easily make this mistake when designing the data structure, A large number of non-alignment accesses seriously affect the system performance.

 

Bytes ------------------------------------------------------------------------------------------------------------------------------

MIPs cpu requires the memory address to be aligned

1. The MIPs CPU can only access the memory through the load/store command.
Generally, the commands of a single command have a single function, and the execution time is relatively fast. Only data operations in registers can be performed, and memory addressing can only be performed through L/s (load/Store. Generally, it is a long command to facilitate the pipeline.
The MIPs is an equivalent-length command of a MIPS system. Each Command has the same length: 32 bits. The operation code is fixed to 6 bits. The other 26 bits are several operands.

2. Alignment of memory addresses
For a 32-bit system, the CPU can only read 32-bit data at a time from the memory. If the CPU needs to read an int type variable and the start position of the variable is not at the top of the 32-bit data, the CPU cannot read the variable at one time, the address of the variable is not aligned. Conversely, if the CPU can read a variable at a time, the address of the variable is aligned.

3. The MIPs cpu requires that the memory address (that is, the load/Store Operation address) be aligned.
In fact, whether it is MIPS or x86, you want the operated address to be aligned, because it can process data most quickly.
However, on the X86 platform, you can easily process non-alignment. However, if MIPS encounters an address non-alignment variable, it will throw an exception to call a large segment of subsequent code, this consumes a lot of time.
Therefore, programmers should develop a good habit of alignment memory addresses no matter what platforms they work on.

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.