【C】pragma pack instruction

1. #pragma pack directive

Compilers on each particular platform have their own default "alignment factor" (also called alignment modulus). Programmers can change this coefficient through the precompilation command, that is, pragma pack(n), n=1, 2, 4, 8, 16, where n is the alignment coefficient you want to specify."

#pragma pack is related to the byte alignment setting of the structure, specifying the alignment of the data in memory.

instruction effect
#pragma pack (n) The C compiler will align according to n bytes, n can take the value of 1, 2, 4, 8, 16
#pragma pack () Cancel custom byte alignment
#pragma pack(push) Push the current number of aligned bytes to the top of the stack without changing the number of aligned bytes
#pragma pack (push,n) It means that the original alignment is set to be pushed onto the stack, and the new alignment is set to n-byte alignment
#pragma pack(pop) Restore the alignment state, pop the number of aligned bytes at the top of the stack, and keep the number of aligned bytes unchanged
#pragma pack(pop,n) Pop the top of the stack and discard it directly, aligned according to n bytes
pragma pack(show) Displays the number of bytes in the current memory alignment, the editor defaults to 8-byte alignment

Adding push and pop can restore the alignment to its original state, instead of the compiler's default. It can be said that the latter is better, but in many cases there is little difference between the two.
For example,

#pragma pack(push)     //保存对齐状态

#pragma pack(4)           //设定为4字节对齐

相当于 #pragma  pack (push,4)  
#pragma  pack (1)   //调整结构体的边界对齐,
//让其以一个字节对齐;<使结构体按1字节方式对齐>

#pragma  pack ()

//例如:

#pragma pack(1)

struct sample
{
    
    
char a;
double b;
};

#pragma pack()

Note:

If it is not enclosed by #pragma pack(1) and #pragma pack(), the sample will be aligned according to the default method of the compiler (the one with the largest size among the members). That is, it is aligned by 8 bytes (double), then sizeof(sample)==16. The member char a occupies 8 bytes (7 of which are null bytes).

If #pragma pack(1) is used, the sample is aligned with sizeof(sample)==9. (no null byte) in 1 byte, which saves space, and some fields can also make the structure easier to control.

2. Application examples

In network protocol programming, data packets of different protocols are often processed. One method is to obtain various information through pointer offset, but this is not only complicated to program, but also troublesome to modify the program once the protocol changes.

After understanding the compiler's allocation principle of the structure space, we can use this feature to define our own protocol structure, and obtain various information by accessing the members of the structure.

This not only simplifies programming, but even if the protocol changes, we only need to modify the definition of the protocol structure, and other programs do not need to be modified, saving time and effort. The following takes the header of the TCP protocol as an example to illustrate how to define the protocol structure. Its protocol structure is defined as follows:

#pragma pack(1) // 按照1字节方式进行对齐
struct TCPHEADER 
{
    
    
     short SrcPort; // 16位源端口号
     short DstPort; // 16位目的端口号
     int SerialNo; // 32位序列号
     int AckNo; // 32位确认号
     unsigned char HaderLen : 4; // 4位首部长度
     unsigned char Reserved1 : 4; // 保留6位中的4位
     unsigned char Reserved2 : 2; // 保留6位中的2位
     unsigned char URG : 1;
     unsigned char ACK : 1;
     unsigned char PSH : 1;
     unsigned char RST : 1;
     unsigned char SYN : 1;
     unsigned char FIN : 1;
     short WindowSize; // 16位窗口大小
     short TcpChkSum; // 16位TCP检验和
     short UrgentPointer; // 16位紧急指针
}; 
#pragma pack()

Guess you like

Origin blog.csdn.net/jiushiguang11/article/details/128717306