Alignment, please be careful!

Source: Internet
Author: User
For those who are used to upper-layer development, alignment, which is an important issue, is often ignored when developing systems with strict format constraints (such as underlying network applications ). Program A bug that is correct but has incorrect results.
In most cases, we always trust the compiler very much and believe that she will give us what we think is binary. Code It is true that in most cases, she is still very obedient, but sometimes she is not satisfied. If you encounter this problem for the first time, it is likely that you will waste most of your time to suspect that it is a code problem.
Recently, this problem has occurred when developing a network program. The following shows the ARP packet structure corresponding to one function:

000001 typedef struct _ dlcheader
000002 {
000003 unsigned char m_receivermac [6];
000004 unsigned char m_sendermac [6];
000005 unsigned short m_ethertype;
000006} dlcheader;
000007 typedef struct _ arpframe
000008 {
000009 unsigned short m_hardwaretype;
000010 unsigned short m_protocoltype;
000011 unsigned char m_lengthmacos;
000012 unsigned char m_lengthip;
000013 unsigned short m_opcode;
000014 unsigned char m_sendermac [6];
000015 unsigned long m_senderip;
000016 unsigned char m_receivermac [6];
000017 unsigned long m_receiverip;
000018} arpframe;

If the number of bytes specified by the struct is strictly 42, the program has no problem at all. However, by default, Visual Studio (. net) is based on the system parameter (bus width) to choose whether to fill in additional data in the data structure during the compilation process to meet the requirements, this process is called alignment. Although the performance is optimized in this way, it is undoubtedly troublesome for us to strictly construct the underlying data packets. The following compares the default alignment and 1byte alignment results:

Uses the default alignment and fills in 10 bytes
Packet Length, captured portion: 62, 62
00000000: 00 16 76 D2 53 E8 00 14 78 97 DC 86 08 00 45 00 .. v. S... X... e.
00000010: 00 30 00 00 40 00 36 06 9f FB 40 D7 A2 40 C0 A8. 0 ...... 6 ...@..@..
00000020: 01 0d 00 50 06 3C A1 D6 BD B9 3B 8B 48 B5 70 12... P... <...;. h. p.
00000030: 16 D0 dd 1D 00 00 02 04 05 AC 01 01 04 02 ..............

Use 1byte alignment to get the correct data packet
Packet Length, captured portion: 42, 42
00000000: 00 16 76 D2 53 E8 01 02 03 04 05 06 08 06 00 01. V. s ...........
00000010: 08 00 06 04 00 01 01 02 03 04 05 06 C0 A8 01 01 ................
00000020: 00 16 76 D2 53 E8 C0 A8 01 0d... v. S .....

Visual Studio provides compilation options for alignment, in [code generation] [structure member alignment (struct member)].

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.