当前位置:网站首页>[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; 边栏推荐
- [interesting reading] deepinf: social influence prediction with deep learning
- PV操作每日一题-售票问题
- rt-thread 使用宝典(2022-0516更新)
- 51定时器初值计算
- vite的public目录
- 山东大学软件学院项目实训-创新实训-网络安全靶场实验平台(十七)
- PV操作每日一题-独木桥问题(变式一)
- 一个独特的简历生成器,开源了!
- xcode-select: error: tool 'xcodebuild' requires Xcode, but active developer directory '/Library/Deve
- 二进制、八进制、十进制、十六进制间互转(整数加小数)
猜你喜欢

VS Code支持配置远程同步了

Concurrent asyncio asynchronous programming

2021 ciscn PWN preliminary

It is very convenient to make a data analysis crosstab with one line of code

单片机触发器或非门工作原理以及用途

Fastadmin uses phpexcel to export tabular data to excel

PV操作每日一题-独木桥问题

How to state clearly and concisely the product requirements?

ADB log fetching

Development of NFT chain game gamefi system and construction of meta universe games
随机推荐
[FAQ] summary of common problems and solutions during the use of rest API interface of sports health service
Leetcode 2000. Reverse word prefix
PV操作每日一题-售票问题
MySQL Foundation
MySQL的体系结构
ADB log fetching
Test question bank and simulation test for operation certificate of ordinary scaffolder (special type of construction work) in 2022
Concurrent asyncio asynchronous programming
Kubernetes setting master schedulable and non schedulable
MySQL - operation database
It is very convenient to make a data analysis crosstab with one line of code
How long has elapsed to show the time
Eg2131 test circuit
怎样简洁明了地说清楚产品需求?
Print: Entry, ':CFBundleIdentifier', Does Not Exist
PAT甲级 1126 欧拉路径
C 12 循环队列Circular queue
塔米狗分享|房地产企业并购只增不减,那么上哪儿找这些项目呢?
FinalShell的下载和使用
[untitled]