user-0 提问: 有关sizeof的用法

回复:
user: user-1
content: 引用 14 楼 u014711608 的回复:[quote=引用 7 楼 zhao4zhong1 的回复:] #include <stdio.h> #define field_offset(s,f) (int)(&(((struct s *)(0))->f)) struct AD  { int a; char b[13]; double c;}; #pragma pack(push) #pragma pack(1) struct A1  { int a; char b[13]; double c;}; #pragma pack(2) struct A2  { int a; char b[13]; double c;}; #pragma pack(4) struct A4  { int a; char b[13]; double c;}; #pragma pack(8) struct A8  { int a; char b[13]; double c;}; #pragma pack(16) struct A16 { int a; char b[13]; double c;}; #pragma pack(pop) int main() {     printf("AD.a %d\n",field_offset(AD,a));     printf("AD.b %d\n",field_offset(AD,b));     printf("AD.c %d\n",field_offset(AD,c));     printf("\n");     printf("A1.a %d\n",field_offset(A1,a));     printf("A1.b %d\n",field_offset(A1,b));     printf("A1.c %d\n",field_offset(A1,c));     printf("\n");     printf("A2.a %d\n",field_offset(A2,a));     printf("A2.b %d\n",field_offset(A2,b));     printf("A2.c %d\n",field_offset(A2,c));     printf("\n");     printf("A4.a %d\n",field_offset(A4,a));     printf("A4.b %d\n",field_offset(A4,b));     printf("A4.c %d\n",field_offset(A4,c));     printf("\n");     printf("A8.a %d\n",field_offset(A8,a));     printf("A8.b %d\n",field_offset(A8,b));     printf("A8.c %d\n",field_offset(A8,c));     printf("\n");     printf("A16.a %d\n",field_offset(A16,a));     printf("A16.b %d\n",field_offset(A16,b));     printf("A16.c %d\n",field_offset(A16,c));     printf("\n");     return 0; } //AD.a 0 //AD.b 4 //AD.c 24 // //A1.a 0 //A1.b 4 //A1.c 17 // //A2.a 0 //A2.b 4 //A2.c 18 // //A4.a 0 //A4.b 4 //A4.c 20 // //A8.a 0 //A8.b 4 //A8.c 24 // //A16.a 0 //A16.b 4 //A16.c 24 // // 老师，能解释一下这句吗#define field_offset(s,f) (int)(&(((struct s *)(0))->f))[/quote] 将0强制类型转换为指向结构s的指针，然后取这个指针指向的结构s的分量f的地址，最后将这个地址强制类型转换为int类型。

user: user-2
content: google或者百度    结构体对齐和补齐

user: user-3
content: LZ可能是学谭浩强的c。 谭将结构体的尺寸算成全部成员之和。 事实上即使在Borland公司的编译器（TC\BC）中就有一编译开关，选择存储优化或运行优化，选用存储优化时，结构体的尺寸小一些，成员间的间隙处要被压缩一些（但也不一定压缩为0）；选用运行优化时，就采用字节对齐方法，成员间存在较多的间隙。

user: user-4
content: 楼主搜一下字节对齐，大把文章介绍。

user: user-5
content: 首先是结构体这个概念，然后是结构体在内存中怎么分布，是否需要内存对齐，对齐的好处是什么，是不是CPU 读取更快，里面的成员怎么计算，顺便和联合体比较一下就很清楚了！

user: user-1
content: #include <stdio.h> #define field_offset(s,f) (int)(&(((struct s *)(0))->f)) struct AD  { int a; char b[13]; double c;}; #pragma pack(push) #pragma pack(1) struct A1  { int a; char b[13]; double c;}; #pragma pack(2) struct A2  { int a; char b[13]; double c;}; #pragma pack(4) struct A4  { int a; char b[13]; double c;}; #pragma pack(8) struct A8  { int a; char b[13]; double c;}; #pragma pack(16) struct A16 { int a; char b[13]; double c;}; #pragma pack(pop) int main() {     printf("AD.a %d\n",field_offset(AD,a));     printf("AD.b %d\n",field_offset(AD,b));     printf("AD.c %d\n",field_offset(AD,c));     printf("\n");     printf("A1.a %d\n",field_offset(A1,a));     printf("A1.b %d\n",field_offset(A1,b));     printf("A1.c %d\n",field_offset(A1,c));     printf("\n");     printf("A2.a %d\n",field_offset(A2,a));     printf("A2.b %d\n",field_offset(A2,b));     printf("A2.c %d\n",field_offset(A2,c));     printf("\n");     printf("A4.a %d\n",field_offset(A4,a));     printf("A4.b %d\n",field_offset(A4,b));     printf("A4.c %d\n",field_offset(A4,c));     printf("\n");     printf("A8.a %d\n",field_offset(A8,a));     printf("A8.b %d\n",field_offset(A8,b));     printf("A8.c %d\n",field_offset(A8,c));     printf("\n");     printf("A16.a %d\n",field_offset(A16,a));     printf("A16.b %d\n",field_offset(A16,b));     printf("A16.c %d\n",field_offset(A16,c));     printf("\n");     return 0; } //AD.a 0 //AD.b 4 //AD.c 24 // //A1.a 0 //A1.b 4 //A1.c 17 // //A2.a 0 //A2.b 4 //A2.c 18 // //A4.a 0 //A4.b 4 //A4.c 20 // //A8.a 0 //A8.b 4 //A8.c 24 // //A16.a 0 //A16.b 4 //A16.c 24 // //

user: user-6
content: 引用 7 楼 zhao4zhong1 的回复:#include <stdio.h> #define field_offset(s,f) (int)(&(((struct s *)(0))->f)) struct AD  { int a; char b[13]; double c;}; #pragma pack(push) #pragma pack(1) struct A1  { int a; char b[13]; double c;}; #pragma pack(2) struct A2  { int a; char b[13]; double c;}; #pragma pack(4) struct A4  { int a; char b[13]; double c;}; #pragma pack(8) struct A8  { int a; char b[13]; double c;}; #pragma pack(16) struct A16 { int a; char b[13]; double c;}; #pragma pack(pop) int main() {     printf("AD.a %d\n",field_offset(AD,a));     printf("AD.b %d\n",field_offset(AD,b));     printf("AD.c %d\n",field_offset(AD,c));     printf("\n");     printf("A1.a %d\n",field_offset(A1,a));     printf("A1.b %d\n",field_offset(A1,b));     printf("A1.c %d\n",field_offset(A1,c));     printf("\n");     printf("A2.a %d\n",field_offset(A2,a));     printf("A2.b %d\n",field_offset(A2,b));     printf("A2.c %d\n",field_offset(A2,c));     printf("\n");     printf("A4.a %d\n",field_offset(A4,a));     printf("A4.b %d\n",field_offset(A4,b));     printf("A4.c %d\n",field_offset(A4,c));     printf("\n");     printf("A8.a %d\n",field_offset(A8,a));     printf("A8.b %d\n",field_offset(A8,b));     printf("A8.c %d\n",field_offset(A8,c));     printf("\n");     printf("A16.a %d\n",field_offset(A16,a));     printf("A16.b %d\n",field_offset(A16,b));     printf("A16.c %d\n",field_offset(A16,c));     printf("\n");     return 0; } //AD.a 0 //AD.b 4 //AD.c 24 // //A1.a 0 //A1.b 4 //A1.c 17 // //A2.a 0 //A2.b 4 //A2.c 18 // //A4.a 0 //A4.b 4 //A4.c 20 // //A8.a 0 //A8.b 4 //A8.c 24 // //A16.a 0 //A16.b 4 //A16.c 24 // // 老师，能解释一下这句吗#define field_offset(s,f) (int)(&(((struct s *)(0))->f))

user: user-7
content: C/C++数据内存对齐，可以参考这篇文章：C++ sizeof的内存计算（1）

user: user-8
content: 好吧。。。 gcc也是这样

user: user-9
content: 按4字节（32位）对齐的

user: user-10
content: 结构体用sizeof计算大小有内存对齐

user: user-11
content: google字节对齐

user: user-12
content: 内存要对齐，对齐的内存必须是最大数据类型的整数倍，本来是9 但是要是4的倍数，就必须是12了

user: user-13
content: 数据对齐 http://www.cnblogs.com/bakari/archive/2012/08/27/2658956.html

user: user-0
content: 引用 4 楼 focuslight 的回复:char和其他结构体成员内存分配大小对齐， 变成4  举个其他例子   sizeof(strcut B)=8不是要按顺序

user: user-14
content: char和其他结构体成员内存分配大小对齐， 变成4  举个其他例子   sizeof(strcut B)=8

user: user-0
content: 引用 2 楼 u014161864 的回复:http://blog.csdn.net/jiejiaozhufu/article/details/8180249  整个结构体大小考虑数据对齐！但是在turbo c编译器下却没有这个对齐

user: user-15
content: http://blog.csdn.net/jiejiaozhufu/article/details/8180249  整个结构体大小考虑数据对齐！

user: user-16
content: 结构体成员对齐