#pragma pack(n)

abzhang2 2007-12-05 05:00:13
#pragma pack(1)
typedef struct tag_AA
{
int m_1; //4
short m_3; //2
char m_4; //1
}AA;
#pragma pack()
////sizeof(AA) = 7
///////////////////////////////////////////////////////////
#pragma pack(2)
typedef struct tag_AB
{
int m_1; //4
short m_3; //2
char m_4; //1
}AB;
#pragma pack()
////sizeof(AB) = 8
/////////////////////////////////////////////////////////
#pragma pack(4)
typedef struct tag_AC
{
int m_1; //4
short m_3; //2
char m_4; //1
}AC;
#pragma pack()
////sizeof(AC) = 8
//////////////////////////////////////////////////////
#pragma pack(8)
typedef struct tag_AD
{
int m_1; //4
short m_3; //2
char m_4; //1
}AD;
#pragma pack()
////sizeof(AD) = 8
////////////////////////////////////////////////////////
#pragma pack(16)
typedef struct tag_AE
{
int m_1; //4
short m_3; //2
char m_4; //1
}AE;
#pragma pack()
////sizeof(AA) = 8
///////////////////////////////////////////////////////
#pragam pack ()这个我知道做什么用,就是恢复默认设置对奇方式
#pragam pack(n)这个怎么理解哦。
pragma pack(1)
int m_1 //[0][1][2][3]
shor m_3 //[4][5]
char m_4 //[6]
so sizeof->7
其他怎么理解哦。。。。它对程序有什么好处啊。
...全文
619 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
abzhang2 2007-12-10
  • 打赏
  • 举报
回复
自己找了一篇文章。觉得还可以。。。不过还是谢谢大家
/*************************************************************
**************************************************************/
#pragma 预处理指令详解
在所有的预处理指令中,#Pragma> 指令可能是最复杂的了,它的作用是设定编译器的状态或者是指示编译器完成一些特定的动作。#pragma指令对每个编译器给出了一个方法,在保持与C和C ++语言完全兼容的情况下,给出主机或操作系统专有的特征。依据定义,编译指示是机器
或操作系统专有的,且对于每个编译器都是不同的。
其格式一般为: #Pragma Para
其中Para 为参数,下面来看一些常用的参数。
(1)message 参数。 Message 参数是我最喜欢的一个参数,它能够在编译信息输出窗
口中输出相应的信息,这对于源代码信息的控制是非常重要的。其使用方法为:
#Pragma message(“消息文本”)
当编译器遇到这条指令时就在编译输出窗口中将消息文本打印出来。
当我们在程序中定义了许多宏来控制源代码版本的时候,我们自己有可能都会忘记有没有正
确的设置这些宏,此时我们可以用这条指令在编译的时候就进行检查。假设我们希望判断自
己有没有在源代码的什么地方定义了_X86这个宏可以用下面的方法
#ifdef _X86
#Pragma message(“_X86 macro activated!”)
#endif
当我们定义了_X86这个宏以后,应用程序在编译时就会在编译输出窗口里显示“_
X86 macro activated!”。我们就不会因为不记得自己定义的一些特定的宏而抓耳挠腮了。
(2)另一个使用得比较多的pragma参数是code_seg。格式如:
#pragma code_seg( [\section-name\[,\section-class\] ] )
它能够设置程序中函数代码存放的代码段,当我们开发驱动程序的时候就会使用到它。
(3)#pragma once (比较常用)
只要在头文件的最开始加入这条指令就能够保证头文件被编译一次,这条指令实际上在VC6
中就已经有了,但是考虑到兼容性并没有太多的使用它。
(4)#pragma hdrstop表示预编译头文件到此为止,后面的头文件不进行预编译。BCB可以预
编译头文件以加快链接的速度,但如果所有头文件都进行预编译又可能占太多磁盘空间,所
以使用这个选项排除一些头文件。
有时单元之间有依赖关系,比如单元A依赖单元B,所以单元B要先于单元A编译。你可以用#p
ragma startup指定编译优先级,如果使用了#pragma package(smart_init) ,BCB就会根据优先级的大小先后编译。
(5)#pragma resource \*.dfm\表示把*.dfm文件中的资源加入工程。*.dfm中包括窗体
外观的定义。
(6)#pragma warning( disable : 4507 34; once : 4385; error : 164 )
等价于:
#pragma warning(disable:4507 34) // 不显示4507和34号警告信息
#pragma warning(once:4385) // 4385号警告信息仅报告一次
#pragma warning(error:164) // 把164号警告信息作为一个错误。
同时这个pragma warning 也支持如下格式:
#pragma warning( push [ ,n ] )
#pragma warning( pop )
这里n代表一个警告等级(1---4)。
#pragma warning( push )保存所有警告信息的现有的警告状态。
#pragma warning( push, n)保存所有警告信息的现有的警告状态,并且把全局警告
等级设定为n。
#pragma warning( pop )向栈中弹出最后一个警告信息,在入栈和出栈之间所作的
一切改动取消。例如:
#pragma warning( push )
#pragma warning( disable : 4705 )
#pragma warning( disable : 4706 )
#pragma warning( disable : 4707 )
//.......
#pragma warning( pop )
在这段代码的最后,重新保存所有的警告信息(包括4705,4706和4707)。
(7)pragma comment(...)
该指令将一个注释记录放入一个对象文件或可执行文件中。
常用的lib关键字,可以帮我们连入一个库文件。如:
#pragma comment(lib, "comctl32.lib")
#pragma comment(lib, "vfw32.lib")
#pragma comment(lib, "wsock32.lib")



每个编译程序可以用#pragma指令激活或终止该编译程序支持的一些编译功能。
例如,对循环优化功能:
#pragma loop_opt(on) // 激活
#pragma loop_opt(off) // 终止
有时,程序中会有些函数会使编译器发出你熟知而想忽略的警告,
如“Parameter xxx is never used in function xxx”,可以这样:
#pragma warn —100 // Turn off the warning message for warning #100
int insert_record(REC *r)
{ /* function body */ }
#pragma warn +100 // Turn the warning message for warning #100 back on
函数会产生一条有唯一特征码100的警告信息,如此可暂时终止该警告。
每个编译器对#pragma的实现不同,在一个编译器中有效在别的编译器中几乎无效。可从编译器的文档中查看。
补充 —— #pragma pack 与内存对齐问题

许多实际的计算机系统对基本类型数据在内存中存放的位置有限制,它们会要求这些数据的首地址的值是某个数k
(通常它为4或8)的倍数,这就是所谓的内存对齐,而这个k则被称为该数据类型的对齐模数(alignment modulus)。
Win32平台下的微软C编译器(cl.exe for 80x86)在默认情况下采用如下的对齐规则:
任何基本数据类型T的对齐模数就是T的大小,即sizeof(T)。比如对于double类型(8字节),
就要求该类型数据的地址总是8的倍数,而char类型数据(1字节)则可以从任何一个地址开始。
Linux下的GCC奉行的是另外一套规则(在资料中查得,并未验证,如错误请指正):
任何2字节大小(包括单字节吗?)的数据类型(比如short)的对齐模数是2,而其它所有超过2字节的数据类型
(比如long,double)都以4为对齐模数。
ANSI C规定一种结构类型的大小是它所有字段的大小以及字段之间或字段尾部的填充区大小之和。
填充区就是为了使结构体字段满足内存对齐要求而额外分配给结构体的空间。那么结构体本身有什么对齐要求吗?
有的,ANSI C标准规定结构体类型的对齐要求不能比它所有字段中要求最严格的那个宽松,可以更严格。

如何使用c/c++中的对齐选项
vc6中的编译选项有 /Zp[1|2|4|8|16] ,/Zp1表示以1字节边界对齐,相应的,/Zpn表示以n字节边界对齐。
n字节边界对齐的意思是说,一个成员的地址必须安排在成员的尺寸的整数倍地址上或者是n的整数倍地址上,取它们中的最小值。
也就是:
min ( sizeof ( member ), n)
实际上,1字节边界对齐也就表示了结构成员之间没有空洞。
/Zpn选项是应用于整个工程的,影响所有的参与编译的结构。
要使用这个选项,可以在vc6中打开工程属性页,c/c++页,选择Code Generation分类,在Struct member alignment可以选择。
要专门针对某些结构定义使用对齐选项,可以使用#pragma pack编译指令:

(1) #pragma pack( [ n ] )
该指令指定结构和联合成员的紧凑对齐。而一个完整的转换单元的结构和联合的紧凑对齐由/Zp 选项设置。
紧凑对齐用pack编译指示在数据说明层设置。该编译指示在其出现后的第一个结构或联合说明处生效。
该编译指示对定义无效。
当你使用#pragma pack ( n ) 时, 这里n 为1、2、4、8 或16。
第一个结构成员之后的每个结构成员都被存储在更小的成员类型或n 字节界限内。
如果你使用无参量的#pragma pack, 结构成员被紧凑为以/Zp 指定的值。该缺省/Zp 紧凑值为/Zp8 。

(2) 编译器也支持以下增强型语法:
#pragma pack( [ [ { push | pop } , ] [ identifier, ] ] [ n] )
若不同的组件使用pack编译指示指定不同的紧凑对齐, 这个语法允许你把程序组件组合为一个单独的转换单元。
带push参量的pack编译指示的每次出现将当前的紧凑对齐存储到一个内部编译器堆栈中。
编译指示的参量表从左到右读取。如果你使用push, 则当前紧凑值被存储起来;
如果你给出一个n 的值, 该值将成为新的紧凑值。若你指定一个标识符, 即你选定一个名称,
则该标识符将和这个新的的紧凑值联系起来。
带一个pop参量的pack编译指示的每次出现都会检索内部编译器堆栈顶的值,并且使该值为新的紧凑对齐值。
如果你使用pop参量且内部编译器堆栈是空的,则紧凑值为命令行给定的值, 并且将产生一个警告信息。
若你使用pop且指定一个n的值, 该值将成为新的紧凑值。若你使用p o p 且指定一个标识符,
所有存储在堆栈中的值将从栈中删除, 直到找到一个匹配的标识符, 这个与标识符相关的紧凑值也从栈中移出,
并且这个仅在标识符入栈之前存在的紧凑值成为新的紧凑值。如果未找到匹配的标识符,
将使用命令行设置的紧凑值, 并且将产生一个一级警告。缺省紧凑对齐为8 。
pack编译指示的新的增强功能让你编写头文件, 确保在遇到该头文件的前后的
紧凑值是一样的。

(3) 栈内存对齐
在vc6中栈的对齐方式不受结构成员对齐选项的影响。它总是保持对齐,而且对齐在4字节边界上

(8)·通过#pragma pack(n)改变C编译器的字节对齐方式
在C语言中,结构是一种复合数据类型,其构成元素既可以是基本数据类型(如int、
long、float等)的变量,也可以是一些复合数据类型(如数组、结构、联合等)的
数据单元。在结构中,编译器为结构的每个成员按其自然对界(alignment)条件分
配空间。各个成员按照它们被声明的顺序在内存中顺序存储,第一个成员的地址和
整个结构的地址相同。
例如,下面的结构各成员空间分配情况:
struct test
{
char x1;
short x2;
float x3;
char x4;
};
结构的第一个成员x1,其偏移地址为0,占据了第1个字节。第二个成员x2为
short类型,其起始地址必须2字节对界,因此,编译器在x2和x1之间填充了一个
空字节。结构的第三个成员x3和第四个成员x4恰好落在其自然对界地址上,在它
们前面不需要额外的填充字节。在test结构中,成员x3要求4字节对界,是该结构
所有成员中要求的最大对界单元,因而test结构的自然对界条件为4字节,编译器
在成员x4后面填充了3个空字节。整个结构所占据空间为12字节。更改C编译器的
缺省字节对齐方式
在缺省情况下,C编译器为每一个变量或是数据单元按其自然对界条件分配
空间。一般地,可以通过下面的方法来改变缺省的对界条件:
  · 使用伪指令#pragma pack (n),C编译器将按照n个字节对齐。
· 使用伪指令#pragma pack (),取消自定义字节对齐方式。
另外,还有如下的一种方式:
· __attribute((aligned (n))),让所作用的结构成员对齐在n字节自然边界上。
如果结构中有成员的长度大于n,则按照最大成员的长度来对齐。
· __attribute__ ((packed)),取消结构在编译过程中的优化对齐,按照实际
占用字节数进行对齐。
以上的n = 1, 2, 4, 8, 16... 第一种方式较为常见。
应用实例
  在网络协议编程中,经常会处理不同协议的数据报文。一种方法是通过指针偏移的
方法来得到各种信息,但这样做不仅编程复杂,而且一旦协议有变化,程序修改起来
也比较麻烦。在了解了编译器对结构空间的分配原则之后,我们完全可以利用这
一特性定义自己的协议结构,通过访问结构的成员来获取各种信息。这样做,
不仅简化了编程,而且即使协议发生变化,我们也只需修改协议结构的定义即可,
其它程序无需修改,省时省力。下面以TCP协议首部为例,说明如何定义协议结构。
其协议结构定义如下:
#pragma pack(1) // 按照1字节方式进行对齐
struct TCPHEADER
{
short SrcPort; // 16位源端口号
short DstPort; // 16位目的端口号
int SerialNo; // 32位序列号
int AckNo; // 32位确认号
unsigned char HaderLen : 4; // 4位首部长度
unsigned char Reserved1 : 4; // 保留6位中的4位
unsigned char Reserved2 : 2; // 保留6位中的2位
unsigned char URG : 1;
unsigned char ACK : 1;
unsigned char PSH : 1;
unsigned char RST : 1;
unsigned char SYN : 1;
unsigned char FIN : 1;
short WindowSize; // 16位窗口大小
short TcpChkSum; // 16位TCP检验和
short UrgentPointer; // 16位紧急指针
};
#pragma pack() // 取消1字节对齐方式
abzhang2 2007-12-10
  • 打赏
  • 举报
回复
我在内存中看了下,知道了那个是空的,但是对于pack(8),pick(16)等一些好像是一样的。
他们对齐到底是什么?
或者说一个概念性的东西,怎么去描述它。 我好分析下是怎么起的。
还不是不明白。。
wshcdr 2007-12-05
  • 打赏
  • 举报
回复
UP
shanhqk 2007-12-05
  • 打赏
  • 举报
回复
这个肯定是不一样的。
内存的对齐方式不样,
特别是在拷贝的时候,完全不一样。除非正好是16的整数倍,这时没有区别,
但其他时候都有区别,

对于这个问题,需要根据实际情况来判断,
打开内存察看一下,就知道那个是空的了
abzhang2 2007-12-05
  • 打赏
  • 举报
回复
to ouyh12345
五岭散人

那么2,4, 8 ,16,基本上是一样的。也就是说设置成,2, 4, 8, 16没区别啊。
我想知道设置成1,2, 4, 8, 16
他占了那些位置。
#pragma pack(1)
AA
{
int m1
short m2
char m4
}
#pragma pack()
sizeof(AA) =7
m1 [0][1][2][3]
m2 [4][5]
m4 [6]

#pragma pack(2)
的时候
sizeof(AA) =8
m1 [0][1][2][3]
m2 [4][5]
m4 [6]
[7] 相当于空
还是
[6] 相当于空
m4 [7]
不知道这样理解正确否。
其他怎么理解。。。。

abzhang2 2007-12-05
  • 打赏
  • 举报
回复
to an_bachelor
MSDN上的,我知道,看过了的。还是不明白,你也难得copy and paste
an_bachelor 2007-12-05
  • 打赏
  • 举报
回复
Collapse AllExpand All Code: All Code: Multiple Code: Visual Basic Code: C# Code: Visual C++ Code: J# Code: JScript
Visual Basic
C#
Visual C++
J#
JScript
C/C++ Preprocessor Reference
pack
Example See Also Send Feedback


Specifies packing alignment for structure, union, and class members.


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


Remarks
pack gives control at the data-declaration level. This differs from compiler option /Zp, which only provides module-level control. pack takes effect at the first struct, union, or class declaration after the pragma is seen; pack has no effect on definitions. Calling pack with no arguments sets n to its default value. This is equivalent to compiler option /Zp8.

Note that if you change the alignment of a structure, the structure will not use as much space in memory, but you may see a decrease in performance or even get a hardware-generated exception for unaligned access. It is possible to modify this exception behavior with SetErrorMode.

show (optional)
Displays the current byte value for packing alignment. The value is displayed by means of a warning message.

push (optional)
Puts a specified packing alignment value, n, on the internal compiler stack, and sets the current packing alignment value to n. If n is not specified, the current packing alignment value is pushed.

pop (optional)
Removes the record from the top of the internal compiler stack. If n is not 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 with identifier, for example, #pragma pack(pop, r1), then all records on the stack are popped until the record with identifier is found, and that record is popped and the packing value associated with the resulting record on the top of is the stack the new packing alignment value. If you pop with an identifier that is not found in any record on the stack, then the pop is ignored.

identifier (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 is not found on the internal stack, nothing is popped.

n(optional)
Specifies the value, in bytes, to be used for packing. The default value for n is 8. Valid values are 1, 2, 4, 8, and 16. The alignment of a member will be on a boundary that is either a multiple of n or a multiple of the size of the member, whichever is smaller.

n can be used with push or pop for setting a particular stack value, or alone for setting the current value used by the compiler.

#pragma pack(pop,identifier, n) is undefined.

For more information on modifying alignment, see:

__alignof

align

__unaligned

Examples of Structure Alignment (x64 specific)

Example
The following sample shows how to the pack pragma to change the alignment of a structure.

Copy Code
// pragma_directives_pack.cpp
#include <stddef.h>
#include <stdio.h>

struct S {
int i; // size 4
short j; // size 2
double k; // size 8
};

#pragma pack(2)
struct T {
int i;
short j;
double k;
};

int main() {
printf("%d ", offsetof(S, i));
printf("%d ", offsetof(S, j));
printf("%d\n", offsetof(S, k));

T tt;
printf("%d ", offsetof(T, i));
printf("%d ", offsetof(T, j));
printf("%d\n", offsetof(T, k));
}

Copy Code
0 4 8
0 4 6


The following sample shows how to use the push, pop, and show syntax.

Copy Code
// pragma_directives_pack_2.cpp
// compile with: /W1 /c
#pragma pack() // n defaults to 8; equivalent to /Zp8
#pragma pack(show) // C4810
#pragma pack(4) // n = 4
#pragma pack(show) // C4810
#pragma pack(push, r1, 16) // n = 16, pushed to stack
#pragma pack(show) // C4810
#pragma pack(pop, r1, 2) // n = 2 , stack popped
#pragma pack(show) // C4810


See Also
Concepts
Pragma Directives and the __Pragma Keyword
Send feedback on this topic to Microsoft.
ouyh12345 2007-12-05
  • 打赏
  • 举报
回复
设字节对齐
我们知道,cpu一次处理一个指令,指令的长为int,
如果某个指令的长度小于int,则需要补齐
abzhang2 2007-12-05
  • 打赏
  • 举报
回复
To stivenjia
什么哦。不明白你的意思。
我看了MSDN,还是不明白
stivenjia 2007-12-05
  • 打赏
  • 举报
回复
MSDN有
#pragma pack( push, before_include1 )
#include "include1.h"
#pragma pack( pop, before_include1 )

16,470

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧