当前位置:网站首页>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)
边栏推荐
- CGroup introduction
- R语言使用gridExtra包的grid.arrange函数将ggplot2包的多个可视化图像横向组合起来,ncol参数自定义组合图列数、nrow参数自定义组合图行数
- Visual Studio 2022下载及配置OpenCV4.5.5
- Quantitative calculation research
- 剑指offer专项32-96题做题笔记
- Sheet1$. Output [excel source output] Error in column [xxx]. The returned column status is: "the text is truncated, or one or more characters have no matches in the target code page.".
- Differences between MySQL Union and union all
- Interview experience in summer camp of Central South University in 2022
- 同事写了一个责任链模式,bug无数...
- Experience container in libvirt
猜你喜欢

GCC compilation process and dynamic link library and static link library

win10 上PHP artisan storage:link 出现 symlink (): Protocol error的解决办法

ArcGIS application (XXI) ArcMap method of deleting layer specified features

Vulnhub geminiinc V2

MCDF Experiment 1

Raven2 of vulnhub

Momentum of vulnhub
![Capturing and sorting out external Fiddler -- Conversation bar and filter [2]](/img/04/e9cc027d753e7049f273d866eefdce.png)
Capturing and sorting out external Fiddler -- Conversation bar and filter [2]

《剑指offer 04》二维数组查找

cgroup简介
随机推荐
Systemverilog-- OOP--对象的拷贝
STL教程9-容器元素深拷贝和浅拷贝问题
Based on MCU, how to realize OTA differential upgrade with zero code and no development?
MySQL searches and sorts out common methods according to time
Concurrent programming - singleton
win10 上PHP artisan storage:link 出现 symlink (): Protocol error的解决办法
【mysql官方文档】死锁
R语言ggplot2可视化:gganimate包创建动态折线图动画(gif)、使用transition_reveal函数在动画中沿给定维度逐步显示数据、在折线移动方向添加数据点
R语言使用原生包(基础导入包、graphics)中的hist函数可视化直方图(histogram plot)
PHP Basics
解决msvcp120d.dll和msvcr120d.dll缺失
Xml的(DTD,xml解析,xml建模)
Nestjs configuration service, configuring cookies and sessions
[learning notes] DP status and transfer
Yintai department store ignites the city's "night economy"
The world's most popular font editor FontCreator tool
vulnhub之tomato(西红柿)
Keepalived中Master和Backup角色选举策略
Visual Studio 2022下载及配置OpenCV4.5.5
Niuniu's team competition