当前位置:网站首页>[C language] pragma pack (1) and pragma pack ()

[C language] pragma pack (1) and pragma pack ()

2022-06-10 10:40:00 Miguel's voice

Set the boundary alignment of the structure to 1 Bytes , That is, all data is stored continuously in memory .

For example, you are in C The following structures are defined in the language :

struct s {
char j;
int i;
};

Then write a sentence in the main function :printf("%d", sizeof(struct s))

That is, the output structure s The number of bytes taken

How much do you think the output will be ?

We know ,char Type take up 1 Bytes ,int Type occupation 4 Bytes , So the output is 5 Do you ?

The answer is No . You can try it yourself , The output is 8.

Why is this so ? This is because the compiler makes the program run faster , Reduce CPU Instruction cycle for reading data , The storage of the structure is optimized . In fact, the first char Although the type member originally had only 1 Bytes , But it actually takes up 4 Bytes , To make the second int The address of a type member can be 4 to be divisible by . So what is actually occupied is 8 Bytes .

and #pragma pack(1) Let the compiler force the structure data to be arranged consecutively , In this case ,sizeof(struct s) The output is zero 5 了 .

Set memory alignment to :1 Byte alignment . How to use it is as follows :

#pragma pack(n)
struct s {
int i;
char j;
};

#pragma pack()
It means The following code is compiled with 1 Bytes aligned . This saves memory resources , But it will affect the efficiency
But , Although it has a certain impact on efficiency , however , If it is protocol based , Such as serial communication program , Then the data packets must be received in strict accordance with certain rules . that , as long as #pragma pack(1), Make data continuous in memory , It's easy to handle .

In protocol programming , Often deal with data packets of different protocols . One method is to get all kinds of information by pointer offset , But doing so is not just programming complex , And once the agreement changes , It's also troublesome to modify the program . After understanding the compiler's allocation principle of structure space , We can use this feature to define our own protocol structure , Get all kinds of information by accessing the members of the structure . To do so , Not only simplifies programming , And even if the agreement changes , We only need to modify the definition of the protocol structure , Other procedures need not be modified , Time saving and labor saving . Let's say TCP Take the first part of the agreement as an example , Explain how to define the protocol structure . Its protocol structure is defined as follows :

#pragma pack(1) // Adjust the boundary alignment of the structure , Align it with one byte ;< Make the structure press 1 Byte alignment >
typedef struct
{
    u8 operatModeConf;
    u8 operatModeValue;
    u8 powerContrModeConf;
    u8 powerContrModeObj;
    u8 powerContrMode;
    u8 signPowAmpModeConf;
    u16 signPowAmpValue;
    u8 beacon1launConf;
    u16 beacon1launValue;
    u8 beacon2launConf;
    u16 beacon2launValue;
    u8 beacon3launConf;
    u16 beacon3launValue;
}WorkCtrlCommand_Def;   
#pragma pack()
extern WorkCtrlCommand_Def *hallWorkCtrlCommand_Def;  

原网站

版权声明
本文为[Miguel's voice]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101016105875.html