#pragma pack可以用来指定C++数据结构的成员变量的内存对齐数值(可选值为1,2,4,8,16)。

本文主要是强调在你的头文件中使用pack指令要配对使用,以避免意外影响项目中其他源文件的结构成员的内存对齐。

如果影响了其他源文件的结构成员内存对齐,那么在你按照默认对齐来计算那些结构成员占用内存大小

或者使用指针移动计算结构成员偏移位置的时候,就可能会出现意料之外的异常。

主要可能的异常是内存定位错误或非法内存访问,结果可能导致错误的定位或数值,极端的情况下可能导致程序崩溃。

下面的例子用来展示基本的配对使用方式。

1)#pragma pack(n)的配对使用

 

 //filename: header1.h

  #pragma pack(1) //内存对齐设置为1个字节

  struct s1
  {
    int i;
    char c;
    bool f;
  } //struct s2{...}   //...   #pragma pack() //恢复默认的内存对齐(与文件开头的指令配对使用)

2)#pragma pack(push|pop,n)的配对使用

  //filename: header2.h

  #pragma pack(push,1) //内存对齐设置为1个字节

  struct s3
  {
    int i;
    char c;
    bool f;
  } //struct s4{...}   //...   #pragma pack(pop) //恢复默认的内存对齐(与文件开头的指令配对使用)

MSDN上VC++2013的帮助文档关于pack指令的用法说明如下,供参考。

-------------------- 以下摘自MSDN ----------------------------

#pragma pack( [ n)

Specifies packing alignment for structure and union members. Whereas the packing alignment of structures and unions is set for an entire translation unit by the /Zp option, the packing alignment is set at the data-declaration level
by the pack pragma. The pragma takes effect at the first structure or union declaration after the pragma is seen; the pragma has no effect on definitions.

When you use #pragma pack(n), where n is 1, 2, 4, 8, or 16, each structure member after the first is stored on the smaller member type or n-byte boundaries. If you use#pragma
pack
 without an argument, structure members are packed to the value specified by /Zp. The default /Zp packing size is /Zp8.

The compiler also supports the following enhanced syntax:

#pragma pack( [ [ { push | pop}, ] [  identifier, ] ] [ n ] )

This syntax allows you to combine program components into a single translation unit if the different components use pack pragmas to specify different packing alignments.

Each occurrence of a pack pragma with a push argument stores the current packing alignment on an internal compiler stack. The pragma’s argument list is read from left to right. If you use push,
the current packing value is stored. If you provide a value for n, that value becomes the new packing value. If you specify an identifier, a name of your choosing, the identifier is associated with the new packing value.

Each occurrence of a pack pragma with a pop argument retrieves the value at the top of an internal compiler stack and makes that value the new packing alignment. If you use pop and
the internal compiler stack is empty, the alignment value is that set from the command-line and a warning is issued. If you use pop and specify a value for n, that value becomes the new packing value. If you use pop and
specify an identifier, all values stored on the stack are removed from the stack until a matchingidentifier is found. The packing value associated with the identifier is also removed from the stack and the packing value that existed
just before the identifier was pushed becomes the new packing value. If no matching identifier is found, the packing value set from the command line is used and a level-one warning is issued. The default packing alignment is 8.

【C/C++开发】C++编译指令#pragma pack的配对使用的更多相关文章

  1. C++编译指令#pragma pack的配对使用

    #pragma pack可以用来指定C++数据结构的成员变量的内存对齐数值(可选值为1,2,4,8,16). 本文主要是强调在你的头文件中使用pack指令要配对使用,以避免意外影响项目中其他源文件的结 ...

  2. stm32中使用#pragma pack(非常有用的字节对齐用法说明)

    #pragma pack(4)   //按4字节对齐,但实际上由于结构体中单个成员的最大占用字节数为2字节,因此实际还是按2字节对齐 typedef struct { char buf[3];//bu ...

  3. #Pragma Pack(n)与内存分配

    #pragma pack(n) 解释一: 每个特定平台上的编译器都有自己的默认"对齐系数"(也叫对齐模数).程序员可以通过预编译命令#pragma pack(n),n=1,2,4, ...

  4. 6.pragma pack

    下面两个结构体 struct One { double d; char c; int i; } struct Two { char c; double d; int i; } 在#pragma pac ...

  5. #pragma pack(n)对齐格式

    #pragma pack(n)对齐格式 #pragma pack(n) 是预处理器用来指定对齐格式的指令,表示n对齐.当元素字节小于n时,要扩展到n:若元素字节大于n则占用其实际大小. struct ...

  6. #Pragma Pack与内存分配

    博客转载自:https://blog.csdn.net/mylinx/article/details/7007309 #pragma pack(n) 解释一: 每个特定平台上的编译器都有自己的默认“对 ...

  7. #pragma编译指令

    #pragma alignment#pragma anon_struct#pragma argsused#pragma checkoption#pragma codeseg#pragma commen ...

  8. C++#pragma pack指令

    微软官方文档说#pragma pack 指令的作用是为结构.联合和类成员指定 pack 对齐.的主要作用就是改变编译器的内存对齐方式,这个指令在网络报文的处理中有着重要的作用,#pragma pack ...

  9. 【VS开发】#pragma pack(push,1)与#pragma pack(1)的区别

    这是给编译器用的参数设置,有关结构体字节对齐方式设置, #pragma pack是指定数据在内存中的对齐方式. #pragma pack (n)             作用:C编译器将按照n个字节对 ...

随机推荐

  1. linux中的alias命令详解

    功能说明:设置指令的别名.语 法:alias[别名]=[指令名称]参 数 :若不加任何参数,则列出目前所有的别名设置.举    例 :ermao@lost-desktop:~$ alias       ...

  2. Prisma 2 is Coming Soon

    转自:https://www.prisma.io/blog/prisma-2-is-coming-soon-mwwfhevie993 Prisma 2 will introduce many fund ...

  3. IIS 站点配置文件

    IIS 站点配置文件  C:/Windows/System32/inetsrv/config/applicationHost.config 配置文件示例: <system.application ...

  4. 控制论模型&心流模型&波模型

    1.控制论模型 这是对设定的目标,通过多次输入和输出,反馈调节,最终达成目标的方法.广泛运用于自然科学与社会科学中.反馈的周期长短决定了调节精度的大小以及达到目标的速度.反馈结果与目标背离的立即纠正, ...

  5. BZOJ 1213: [HNOI2004]高精度开根

    二次联通门 : BZOJ 1213: [HNOI2004]高精度开根 正解 NTT+高精+倍增+二分 但是可以用python 2333333 m,n=int(raw_input()),int(raw_ ...

  6. SQL基础-过滤数据

    一.过滤数据 1.使用WHERE子句 过滤数据:关键字WHERE SELECT 字段列表 FROM 表名 WHERE 过滤条件; 过滤条件一般由要过滤的字段.操作符.限定值三部分组成: 如: SELE ...

  7. Apache ranger整合hive报错记录

    版本信息如下: hadoop2.9.2 hive 2.x ranger 最新版2.1.0 在hive端部署完ranger 插件以后,在使用beeline连接查询数据库时报错,报错信息如下: verbo ...

  8. koa post提交数据,koa-bodyparser中间件来获取post提交数据

    原生 Nodejs 获取 post 提交数据 首先创建并初始化一个node应用,根路由使用index.ejs模板 var Koa=require('koa') var router = require ...

  9. SDN上机第三次作业

    1. 利用Mininet仿真平台构建如下图所示的网络拓扑,配置主机h1和h2的IP地址(h1:10.0.0.1,h2:10.0.0.2),测试两台主机之间的网络连通性 使用miniedit进行创建操作 ...

  10. iview 的事件绑定

    iview 内的组件样式是不错,有时候我们想用它且绑定某个事件: 比如,我们使用了步骤条组件(Steps),然后绑定点击事件,实现每次点击某个步骤条内的step 就显示此step的具体信息, < ...