当前位置:网站首页>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)
边栏推荐
- C language utf8toutf16 (UTF-8 characters are converted to hexadecimal encoding)
- Differences between MySQL Union and union all
- Dynamically monitor disk i/o with ZABBIX
- 《剑指offer 03》数组中重复的数字
- vulnhub之cereal
- Simple factory and factory method mode
- Hongmeng fourth training
- Mysql根据时间搜索常用方法整理
- 安裝electron失敗的解决辦法
- typeScript
猜你喜欢
随机推荐
Notes on 32-96 questions of sword finger offer
OpenGL 着色器使用
DNS multi-point deployment IP anycast+bgp actual combat analysis
How to mix embedded MCU, arm and DSP?
"Jianzhi offer 04" two-dimensional array search
Vulnhub's cereal
The excel table is transferred to word, and the table does not exceed the edge paper range
Based on MCU, how to realize OTA differential upgrade with zero code and no development?
Hongmeng fourth training
Momentum of vulnhub
MCDF Experiment 1
shardingSphere分库分表<3>
OpenGL 索引缓存对象EBO和线宽模式
R语言使用gridExtra包的grid.arrange函数将lattice包的多个可视化图像横向组合起来,ncol参数自定义组合图列数、nrow参数自定义组合图行数
Excel快速跨表复制粘贴
安装electron失败的解决办法
Oracle advanced (I) realize DMP by expdp impdp command
Vulnhub geminiinc
vulnhub之Nagini
Unity3d learning notes 5 - create sub mesh