博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【C/C++开发】C++编译指令#pragma pack的配对使用
阅读量:5340 次
发布时间:2019-06-15

本文共 3100 字,大约阅读时间需要 10 分钟。

#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.

转载于:https://www.cnblogs.com/huty/p/8517064.html

你可能感兴趣的文章
Spring Cloud Stream消费失败后的处理策略(三):使用DLQ队列(RabbitMQ)
查看>>
bzoj1048 [HAOI2007]分割矩阵
查看>>
Java中的编码
查看>>
PKUWC2018 5/6
查看>>
As-If-Serial 理解
查看>>
洛谷P1005 矩阵取数游戏
查看>>
在Silverlight中使用HierarchicalDataTemplate为TreeView实现递归树状结构
查看>>
无线通信基础(一):无线网络演进
查看>>
如何在工作中快速成长?阿里资深架构师给工程师的10个简单技巧
查看>>
WebSocket 时时双向数据,前后端(聊天室)
查看>>
关于python中带下划线的变量和函数 的意义
查看>>
linux清空日志文件内容 (转)
查看>>
安卓第十三天笔记-服务(Service)
查看>>
Servlet接收JSP参数乱码问题解决办法
查看>>
【bzoj5016】[Snoi2017]一个简单的询问 莫队算法
查看>>
Ajax : load()
查看>>
MySQL-EXPLAIN执行计划Extra解释
查看>>
Zookeeper概述
查看>>
Zookeeper一致性级别
查看>>
Linux远程登录
查看>>