Byte alignment #pragma pack

This parameter is used by the compiler for the relevant byte alignment structure is provided, #pragma pack specifying data alignment in memory.

#pragma pack (n) action: C compiler aligned in n bytes.
#pragma pack () action: Cancel Custom byte alignment.


#pragma pack (push, 1) effect: refers to the original alignment of the push, and set the new alignment is set to a byte alignment

#pragma pack (pop) effect: to restore alignment status

So visible, adding push and pop can be aligned restored to its original state, rather than the default compiler can be said that the latter better, but a lot of the time difference is small

Such as:

#pragma pack (push) // save aligned state

#pragma pack (4) // set to 4-byte alignment

  Corresponds #pragma pack (push, 4)  

 

#pragma pack (1) effect: to adjust the boundary structure is aligned to let a byte alignment; <1 that the structure of byte-aligned>

#pragma pack ()

E.g:

#pragma pack (1)

struct sample
{
char a;
double b;
};

#pragma pack ()

Note: If no #pragma pack (1) and #pragma pack () enclosed, the sample alignment (members of the largest size) by the compiler default. I.e., 8 bytes (double) aligned, sizeof (sample) == 16 char a member holding eight bytes (seven of which are null bytes);. If using #pragma pack (1), according to the sample 1 byte alignment sizeof (sample) == 9. (no null bytes), comparing it to save space, and some fields may also make the structure easier to control.

Applications

In the network protocol programming, often deal with data packets of different protocols. One method is to obtain various kinds of information by means of a pointer offset, but this is not only complicated programming, and once the protocol changes, it is relatively cumbersome to modify. In the understanding of the principle of distribution compiler construction space, we can use this feature to define your own protocol structure, to obtain a variety of information access by members of the structure. In doing so, not only simplifies programming, and even if the agreement is changed, we also need to define the structure of the modified agreement, without modifying other programs, saving time and effort. TCP protocol headers below to illustrate how to define the protocol structure. Protocol structure which is defined as follows: 

#pragma pack (1) // 1-byte aligned in accordance
struct TCPHEADER 
{
     Short SrcPort; // 16-bit source port number of
     short DstPort; // 16-bit destination port number
     int SerialNo; // 32-bit serial number
     int AckNo; // 32-bit acknowledgment number
     unsigned char HaderLen: 4; // 4 bit header length
     unsigned char Reserved1: 4; // reserved 4 6 in
     unsigned char Reserved2: 2; // reserved 6 in two
     unsigned char the URG:. 1;
     unsigned char the ACK:. 1;
     unsigned char PSH:. 1;
     unsigned char the RST:. 1;
     unsigned char the SYN:. 1;
     unsigned char the FIN:. 1;
     Short the WindowSize; // 16-bit window size
     short TcpChkSum; // 16 bit TCP checksum
     short UrgentPointer; // 16 bit urgent pointer
}; 
#pragma Pack ()

Guess you like

Origin www.cnblogs.com/mingzhang/p/11263441.html