pragma pack語法:
#pragma pack( [ n ] )
n - 選填温治,代表用于包裝的字節(jié)數(shù),如果沒有設(shè)置編譯器選項(xiàng)/Zp
戒悠,n
默認(rèn)是8. n
可以是1熬荆,2,4绸狐,8 和16卤恳。成員對齊在某個(gè)邊界上,該邊界為n的倍數(shù)或成員大小的倍數(shù)(以較小者為準(zhǔn))寒矿。
例1:邊界為n的倍數(shù)
此例中#pragma pack(1)
之后的結(jié)構(gòu)體T和Q的成員起始邊界是1字節(jié)對齊的突琳。
#include <stdio.h>
typedef struct {
int i; // size 4, offset 0
char j; // size 1, offset 4
double k; // size 8, offset 8
} S;
#pragma pack(1)
typedef struct{
int i; // size 4, offset 0
char j; // size 1, offset 4
double k; // size 8, but offset 5
} T ;
typedef struct{
int i; // size 4, offset 0
char j; // size 1, offset 4
double k; // size 8, but offset 5
} Q ;
#define member_size(type, member) sizeof(((type *)0)->member)
#define member_offset(type, member) (long unsigned int) &(((type *)0)->member)
int main()
{
printf("sizeof(S)=%lu\n", sizeof(S));
printf("offset(S->i)=%lu\n", member_offset(S,i));
printf("sizeof(S->i)=%lu\n", member_size(S,i));
printf("\n");
printf("offset(S->j)=%lu\n", member_offset(S,j));
printf("sizeof(S->j)=%lu\n", member_size(S,j));
printf("\n");
printf("offset(S->k)=%lu\n", member_offset(S,k));
printf("sizeof(S->k)=%lu\n", member_size(S,k));
printf("\n");
printf("sizeof(T)=%lu\n", sizeof(T));
printf("offset(T->i)=%lu\n", member_offset(T,i));
printf("sizeof(T->i)=%lu\n", member_size(T,i));
printf("\n");
printf("offset(T->j)=%lu\n", member_offset(T,j));
printf("sizeof(T->j)=%lu\n", member_size(T,j));
printf("\n");
printf("offset(T->k)=%lu\n", member_offset(T,k));
printf("sizeof(T->k)=%lu\n", member_size(T,k));
printf("\n");
printf("sizeof(Q)=%lu\n", sizeof(Q));
printf("offset(Q->i)=%lu\n", member_offset(Q,i));
printf("sizeof(Q->i)=%lu\n", member_size(Q,i));
printf("\n");
printf("offset(Q->j)=%lu\n", member_offset(Q,j));
printf("sizeof(Q->j)=%lu\n", member_size(Q,j));
printf("\n");
printf("offset(Q->k)=%lu\n", member_offset(Q,k));
printf("sizeof(Q->k)=%lu\n", member_size(Q,k));
return 0;
}
輸出結(jié)果:
sizeof(S)=16
offset(S->i)=0
sizeof(S->i)=4
offset(S->j)=4
sizeof(S->j)=1
offset(S->k)=8
sizeof(S->k)=8
sizeof(T)=13
offset(T->i)=0
sizeof(T->i)=4
offset(T->j)=4
sizeof(T->j)=1
offset(T->k)=5
sizeof(T->k)=8
sizeof(Q)=13
offset(Q->i)=0
sizeof(Q->i)=4
offset(Q->j)=4
sizeof(Q->j)=1
offset(Q->k)=5
sizeof(Q->k)=8
例2:#pragma pack()默認(rèn)取較小成員大小的倍數(shù)
將上面例子中結(jié)構(gòu)體T之后加上#pragma pack()
,那么1字節(jié)對齊只適用于T,而不包含Q符相。一般程序中拆融,#pragma pack(1)
和#pragma pack()
成對出現(xiàn)。
#include <stdio.h>
typedef struct {
int i; // size 4, offset 0
char j; // size 1, offset 4
double k; // size 8, offset 8
} S;
#pragma pack(1)
typedef struct{
int i; // size 4, offset 0
char j; // size 1, offset 4
double k; // size 8, but offset 5
} T ;
#pragma pack()
typedef struct{
int i; // size 4, offset 0
char j; // size 1, offset 4
double k; // size 8, offset 8
} Q ;
#define member_size(type, member) sizeof(((type *)0)->member)
#define member_offset(type, member) (long unsigned int) &(((type *)0)->member)
int main()
{
printf("sizeof(S)=%lu\n", sizeof(S));
printf("offset(S->i)=%lu\n", member_offset(S,i));
printf("sizeof(S->i)=%lu\n", member_size(S,i));
printf("\n");
printf("offset(S->j)=%lu\n", member_offset(S,j));
printf("sizeof(S->j)=%lu\n", member_size(S,j));
printf("\n");
printf("offset(S->k)=%lu\n", member_offset(S,k));
printf("sizeof(S->k)=%lu\n", member_size(S,k));
printf("\n");
printf("sizeof(T)=%lu\n", sizeof(T));
printf("offset(T->i)=%lu\n", member_offset(T,i));
printf("sizeof(T->i)=%lu\n", member_size(T,i));
printf("\n");
printf("offset(T->j)=%lu\n", member_offset(T,j));
printf("sizeof(T->j)=%lu\n", member_size(T,j));
printf("\n");
printf("offset(T->k)=%lu\n", member_offset(T,k));
printf("sizeof(T->k)=%lu\n", member_size(T,k));
printf("\n");
printf("sizeof(Q)=%lu\n", sizeof(Q));
printf("offset(Q->i)=%lu\n", member_offset(Q,i));
printf("sizeof(Q->i)=%lu\n", member_size(Q,i));
printf("\n");
printf("offset(Q->j)=%lu\n", member_offset(Q,j));
printf("sizeof(Q->j)=%lu\n", member_size(Q,j));
printf("\n");
printf("offset(Q->k)=%lu\n", member_offset(Q,k));
printf("sizeof(Q->k)=%lu\n", member_size(Q,k));
return 0;
}
輸出結(jié)果:
sizeof(S)=16
offset(S->i)=0
sizeof(S->i)=4
offset(S->j)=4
sizeof(S->j)=1
offset(S->k)=8
sizeof(S->k)=8
sizeof(T)=13
offset(T->i)=0
sizeof(T->i)=4
offset(T->j)=4
sizeof(T->j)=1
offset(T->k)=5
sizeof(T->k)=8
sizeof(Q)=16
offset(Q->i)=0
sizeof(Q->i)=4
offset(Q->j)=4
sizeof(Q->j)=1
offset(Q->k)=8
sizeof(Q->k)=8