当前位置:网站首页>pragma-pack语法与使用
pragma-pack语法与使用
2022-07-03 11:11:00 【春夜喜雨】
(Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu)
参考:https://docs.microsoft.com/en-us/cpp/preprocessor/pack?view=msvc-170
参考:https://gcc.gnu.org/onlinedocs/gcc-4.4.4/gcc/Structure_002dPacking-Pragmas.html
参考:https://blog.shengbin.me/posts/gcc-attribute-aligned-and-packed
参考:https://gcc.gnu.org/onlinedocs/gcc-7.5.0/gcc/Structure-Layout-Pragmas.html#Structure-Layout-Pragmas
pragma-pack通常是我们做涉及IO程序,或涉及通信程序时,会对结构体或类做的设定。通常我们考虑着网络通信时或io上尽可能节省字节长度。有时,也可能考虑cpu特点或考虑计算运行效率,对对齐方式做指定。
对齐指令主要影响的是结构体/类内部变量的字节对齐情况,在不同对齐指令下,可能结构体的长度会发生改变。
pack语法说明
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) Displays the current byte value for packing alignment. The value is displayed by a warning message.
使用样例:#pragma pack(show)
push:(可选)首先把当前对齐字节push到中间编译器栈中,然后设定当前的对齐字节为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.
使用样例:#pragma pack(push, 1)
使用样例:#pragma pack (push, r1, 2) 相当于 #pragma pack(push, r1) -> #pragma pack(2)
pop:(可选)从中间编译器栈顶取出元素,并设定性的对齐字节
(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).
使用样例:#pragma pop()
使用样例:#pragma pack (pop, 2) 相当于 #pragma pack(pop) -> #pragma pack(2)
identifier: (可选)标识名称;当pop时,如果标识名称在中间编译器栈上没找到,不会pop任何元素,相当于无效;
(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) 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.
配套使用场景
设定当前值/恢复缺省值
// 设定当前对齐字节
#pragma pack(8)
…
// 设定缺省对齐
#pragma pack()
配套:设定对齐,并恢复之前对齐
#pragma pack(push, 8)
…
#pragma pack(pop)
配套:带命名,设定对齐方式,并恢复之前对齐方式
#pragma pack(push, NamedAAA, 8)
…
#pragma pack(pop, NamedAAA)
使用注意:
- 通常我们仅考虑对项目内的自定义结构体或特定类设定对齐方式;对于项目外的内容通常不做指定,避免关联影响。
- 上述pack语法vs均可以支持,但是对于linux下gcc编译器,支持的语法比这个要少一些,不支持pack(show)语法;
(Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu)
边栏推荐
- Raven2 of vulnhub
- "Jianzhi offer 04" two-dimensional array search
- Modular programming of single chip microcomputer
- 836. Merge sets (day 63) and search sets
- STL tutorial 10 container commonalities and usage scenarios
- STL教程8-map
- Excel快速跨表复制粘贴
- R language uses grid of gridextra package The array function combines multiple visual images of the lattice package horizontally, and the ncol parameter defines the number of columns of the combined g
- . \vmware-vdiskmanager. exe -k “c:\\xxxxx.vmdk”
- previous permutation lintcode51
猜你喜欢
Software testing weekly (issue 78): the more confident you are about the future, the more patient you are about the present.
vulnhub之presidential
Yintai department store ignites the city's "night economy"
This article explains the complex relationship between MCU, arm, MCU, DSP, FPGA and embedded system
银泰百货点燃城市“夜经济”
错排问题 (抽奖,发邮件)
Vulnhub's Tomato (tomato)
vulnhub之pyexp
Kubernetes three dozen probes and probe mode
Xiaopeng P7 hit the guardrail and the airbag did not pop up. The official responded that the impact strength did not meet the ejection requirements
随机推荐
Vulnhub's Tomato (tomato)
shardingSphere分库分表<3>
"Jianzhi offer 04" two-dimensional array search
Go language to realize static server
Solution à la défaillance de l'installation d'Electron
VS2015的下载地址和安装教程
PHP基础
同事写了一个责任链模式,bug无数...
Visual Studio 2022下载及配置OpenCV4.5.5
The excel table is transferred to word, and the table does not exceed the edge paper range
The R language uses the hist function in the native package (basic import package, graphics) to visualize the histogram plot
STL tutorial 8-map
rxjs Observable filter Operator 的实现原理介绍
STL Tutorial 9 deep copy and shallow copy of container elements
mysql使用update联表更新的方法
How should intermediate software designers prepare for the soft test
CGroup introduction
Solution to the second weekly test of ACM intensive training of Hunan Institute of technology in 2022
Yintai department store ignites the city's "night economy"
previous permutation lintcode51