当前位置:网站首页>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)
- Modular programming of single chip microcomputer
- 2022年湖南工学院ACM集训第二次周测题解
- vulnhub之narak
- 【mysql专项】读锁和写锁
- Based on MCU, how to realize OTA differential upgrade with zero code and no development?
- DNS multi-point deployment IP anycast+bgp actual combat analysis
- OpenStack中的测试分类
- OpenGL 着色器使用
- MCDF Experiment 1
猜你喜欢

鸿蒙第三次培训(项目实训)

During FTP login, the error "530 login incorrect.login failed" is reported

vulnhub之presidential

PHP基础

Hongmeng third training (project training)

ftp登录时,报错“530 Login incorrect.Login failed”

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

2022年湖南工学院ACM集训第二次周测题解

Momentum of vulnhub

win10 上PHP artisan storage:link 出现 symlink (): Protocol error的解决办法
随机推荐
Hongmeng third training (project training)
typeScript
Qt OpenGL相机的使用
OPenGL 基本知识(根据自己理解整理)
STL教程9-容器元素深拷贝和浅拷贝问题
MySQL uses the method of updating linked tables with update
Kibana - installation and configuration of kibana
libvirt 中体验容器
Go语言实现静态服务器
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
ArcGIS application (XXI) ArcMap method of deleting layer specified features
2022年中南大学夏令营面试经验
Nestjs configuration service, configuring cookies and sessions
PHP Basics
STL教程10-容器共性和使用场景
Keepalived中Master和Backup角色选举策略
MCDF实验1
在CoreOS下部署WordPress实例教程
Modular programming of single chip microcomputer
PHP导出word方法(一phpword)