当前位置:网站首页>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)
边栏推荐
- rxjs Observable filter Operator 的实现原理介绍
- R language uses the aggregate function to calculate the mean value (sum) of dataframe data grouping aggregation without setting na The result of RM calculation. If the group contains the missing value
- R语言使用原生包(基础导入包、graphics)中的hist函数可视化直方图(histogram plot)
- STL教程10-容器共性和使用场景
- Visual studio 2022 downloading and configuring opencv4.5.5
- 836. Merge sets (day 63) and search sets
- VS2015的下载地址和安装教程
- Test classification in openstack
- Kubernetes 三打探针及探针方式
- R语言使用aggregate函数计算dataframe数据分组聚合的均值(sum)、不设置na.rm计算的结果、如果分组中包含缺失值NA则计算结果也为NA
猜你喜欢
[learning notes] DP status and transfer
Vulnhub geminiinc
Duplicate numbers in the array of sword finger offer 03
shardingSphere分库分表<3>
Vulnhub narak
This article explains the complex relationship between MCU, arm, MCU, DSP, FPGA and embedded system
同事写了一个责任链模式,bug无数...
GCC compilation process and dynamic link library and static link library
STL教程9-容器元素深拷贝和浅拷贝问题
【学习笔记】dp 状态与转移
随机推荐
R语言使用gridExtra包的grid.arrange函数将lattice包的多个可视化图像横向组合起来,ncol参数自定义组合图列数、nrow参数自定义组合图行数
laravel 时区问题timezone
Excel表格转到Word中,表格不超边缘纸张范围
《剑指offer 04》二维数组查找
DNS多点部署IP Anycast+BGP实战分析
R语言ggplot2可视化:gganimate包创建动态折线图动画(gif)、使用transition_reveal函数在动画中沿给定维度逐步显示数据、在折线移动方向添加数据点
Visual Studio 2022下载及配置OpenCV4.5.5
cgroup简介
Is BigDecimal safe to calculate the amount? Look at these five pits~~
网络通讯之Socket-Tcp(一)
Colleagues wrote a responsibility chain model, with countless bugs
安装electron失败的解决办法
Notes on 32-96 questions of sword finger offer
"Jianzhi offer 04" two-dimensional array search
2022年湖南工学院ACM集训第二次周测题解
The R language uses the hist function in the native package (basic import package, graphics) to visualize the histogram plot
Vulnhub's presidential
Xml的(DTD,xml解析,xml建模)
Differences between MySQL Union and union all
previous permutation lintcode51