当前位置:网站首页>Pragma pack syntax and usage
Pragma pack syntax and usage
2022-07-03 11:57:00 【Rainy spring night】
(Owed by: Happy rain in spring night http://blog.csdn.net/chunyexiyu)
Reference resources :https://docs.microsoft.com/en-us/cpp/preprocessor/pack?view=msvc-170
Reference resources :https://gcc.gnu.org/onlinedocs/gcc-4.4.4/gcc/Structure_002dPacking-Pragmas.html
Reference resources :https://blog.shengbin.me/posts/gcc-attribute-aligned-and-packed
Reference resources :https://gcc.gnu.org/onlinedocs/gcc-7.5.0/gcc/Structure-Layout-Pragmas.html#Structure-Layout-Pragmas
pragma-pack Usually we do things involving IO Program , Or when it comes to communication procedures , Will set the structure or class . Usually we consider network communication or io Save byte length as much as possible . Sometimes , May also consider cpu Characteristics or consider the calculation of operation efficiency , Specify the alignment .
The alignment instruction mainly affects the structure / Byte alignment of variables inside the class , Under different alignment instructions , The length of the structure may change .
pack Syntax description
Specifies the packing alignment for structure, union, and class members.
Syntax
#pragma pack( show )
#pragma pack( push [ , identifier ] [ , n ] )
#pragma pack( pop [ , { identifier | n } ] )
#pragma pack( [ n ] )
show:( Optional ) Display the current aligned bytes
(Optional) Displays the current byte value for packing alignment. The value is displayed by a warning message.
Use samples :#pragma pack(show)
push:( Optional ) First align the current byte push To the intermediate compiler stack , Then set the current alignment byte to n.
(Optional) Pushes the current packing alignment value on the internal compiler stack, and sets the current packing alignment value to n.
If n isn’t specified, the current packing alignment value is pushed.
Use samples :#pragma pack(push, 1)
Use samples :#pragma pack (push, r1, 2) amount to #pragma pack(push, r1) -> #pragma pack(2)
pop:( Optional ) Take the element from the top of the intermediate compiler stack , And set the alignment bytes
(Optional) Removes the record from the top of the internal compiler stack. If n isn’t specified with pop, then the packing value associated with the resulting record on the top of the stack is the new packing alignment value. If n is specified, for example, #pragma pack(pop, 16), n becomes the new packing alignment value. If you pop using an identifier, for example, #pragma pack(pop, r1), then all records on the stack are popped until the record that has identifier is found. That record gets popped, and the packing value associated with the record found on the top of the stack becomes the new packing alignment value. If you pop using an identifier that isn’t found in any record on the stack, then the pop is ignored.
The statement #pragma pack (pop, r1, 2) is equivalent to #pragma pack (pop, r1) followed by #pragma pack(2).
Use samples :#pragma pop()
Use samples :#pragma pack (pop, 2) amount to #pragma pack(pop) -> #pragma pack(2)
identifier: ( Optional ) Logo name ; When pop when , If the identification name is not found on the intermediate compiler stack , Can't pop Any element , Equivalent to invalid ;
(Optional) When used with push, assigns a name to the record on the internal compiler stack. When used with pop, pops records off the internal stack until identifier is removed. If identifier isn’t found on the internal stack, nothing is popped.
n:( Optional ) Align bytes
(Optional) Specifies the value, in bytes, to be used for packing. If the compiler option /Zp isn’t set for the module, the default value for n is 8. Valid values are 1, 2, 4, 8, and 16. The alignment of a member is on a boundary that’s either a multiple of n, or a multiple of the size of the member, whichever is smaller.
Supporting use scenarios
Set the current value / Restore default values
// Set the current alignment byte
#pragma pack(8)
…
// Set the default alignment
#pragma pack()
Matching : Set alignment , And restore the previous alignment
#pragma pack(push, 8)
…
#pragma pack(pop)
Matching : With naming , Set alignment , And restore the previous alignment
#pragma pack(push, NamedAAA, 8)
…
#pragma pack(pop, NamedAAA)
Use caution :
- Usually, we only consider setting the alignment of custom structures or specific classes in the project ; Content outside the project is usually not specified , Avoid correlation effects .
- Above pack grammar vs Can support , But for linux Next gcc compiler , The supported syntax is less than this , I won't support it pack(show) grammar ;
(Owed by: Happy rain in spring night http://blog.csdn.net/chunyexiyu)
边栏推荐
- This article explains the complex relationship between MCU, arm, MCU, DSP, FPGA and embedded system
- During FTP login, the error "530 login incorrect.login failed" is reported
- 《剑指offer 03》数组中重复的数字
- 网络通讯之Socket-Tcp(一)
- vulnhub之momentum
- XML (DTD, XML parsing, XML modeling)
- Interview experience in summer camp of Central South University in 2022
- Hongmeng third training (project training)
- Extrapolated scatter data
- R language ggplot2 visualization: gganimate package creates dynamic line graph animation (GIF) and uses transition_ The reveal function displays data step by step along a given dimension in the animat
猜你喜欢
Modular programming of single chip microcomputer
After watching the video, AI model learned to play my world: cutting trees, making boxes, making stone picks, everything is good
《剑指offer 04》二维数组查找
Kubernetes three dozen probes and probe mode
OPenGL 基本知识(根据自己理解整理)
Solution to the second weekly test of ACM intensive training of Hunan Institute of technology in 2022
同事写了一个责任链模式,bug无数...
银泰百货点燃城市“夜经济”
Web安全总结
Vulnhub's Tomato (tomato)
随机推荐
R语言使用gridExtra包的grid.arrange函数将lattice包的多个可视化图像横向组合起来,ncol参数自定义组合图列数、nrow参数自定义组合图行数
抓包整理外篇fiddler———— 会话栏与过滤器[二]
Deploying WordPress instance tutorial under coreos
VS2015的下载地址和安装教程
简单工厂和工厂方法模式
MCDF实验1
typeScript
Uniapp implementation Click to load more
【学习笔记】dp 状态与转移
The uniapp scroll view solves the problems of high adaptability and bullet frame rolling penetration.
Groovy test class and JUnit test
Solve msvcp120d DLL and msvcr120d DLL missing
Web安全总结
How to get started embedded future development direction of embedded
Slam mapping and autonomous navigation simulation based on turnlebot3
Duplicate numbers in the array of sword finger offer 03
How to mix embedded MCU, arm and DSP?
Go language to realize static server
OpenGL 索引缓存对象EBO和线宽模式
shardingSphere分库分表<3>