pragma 的问题

DuBin11 2009-01-15 11:18:30
#pragma pack(1)
typedef struct
{
char a;
int b;
}firststruc;

#pragma pack(2)
typedef struct
{
char a;
short b;
int c;
}secondstruc;
sizeof(firststruc)和sizeof(secondstruc)分别为
这里的#pragma pack(1)和#pragma pack(2)分别表示什么

...全文
182 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
lingyin55 2009-02-03
  • 打赏
  • 举报
回复
边界对齐,和结构体内变量的存储顺序也有关系的。
giant7 2009-02-02
  • 打赏
  • 举报
回复
学习了!!!!
lockhall 2009-02-01
  • 打赏
  • 举报
回复
强制按照N字节对齐

不然一般32位操作系统下,按照4字节对齐原则!
捕鲸叉 2009-02-01
  • 打赏
  • 举报
回复
边界对齐
pauliuyou 2009-01-20
  • 打赏
  • 举报
回复
学习了, 不错.
lbh2001 2009-01-15
  • 打赏
  • 举报
回复
[Quote=引用楼主 DuBin11 的帖子:]
#pragma pack(1)
typedef struct
{
char a;
int b;
}firststruc;

#pragma pack(2)
typedef struct
{
char a;
short b;
int c;
}secondstruc;
sizeof(firststruc)和sizeof(secondstruc)分别为
这里的#pragma pack(1)和#pragma pack(2)分别表示什么
[/Quote]
32位下
sizeof(firststruc)为5 ---> 1 + 4
sizeof(secondstruc)为8 ---> 1 + 1(填充的一位) + 2 + 4
xiaoyisnail 2009-01-15
  • 打赏
  • 举报
回复
#pragma pack(1)
typedef struct
{
char a;
int b;
}firststruc;

此时struct firststruc的大小可以任意,因为都是一字节的整数倍,编译器不会再去对齐调整了

#pragma pack(2)
typedef struct
{
char a;
short b;
int c;
}secondstruc;
此时struct secondstruc会被编译器进行对齐,大小被调整为2的整数倍,在大多数32位机器上,sizeof(secondstruc)为8
waizqfor 2009-01-15
  • 打赏
  • 举报
回复
使用伪指令#pragma pack (n),C编译器将按照n个字节对齐。
使用伪指令#pragma pack (),取消自定义字节对齐方式。
与你申请的结构体有关系
九桔猫 2009-01-15
  • 打赏
  • 举报
回复
这个和内存对齐有关。
给楼主看下MSDN上的有关解释吧

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

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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