user-0 提问: c语言 内存对其问题

回复:
user: user-1
content: 也可以手动指定对齐方式， 请百度    “C++ 位域”

user: user-2
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("AD sizeof %d\n", sizeof(AD));     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("A1 sizeof %d\n", sizeof(A1));     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("A2 sizeof %d\n", sizeof(A2));     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("A4 sizeof %d\n", sizeof(A4));     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("A8 sizeof %d\n", sizeof(A8));     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("A16 sizeof %d\n", sizeof(A16));     printf("\n");     return 0; } //AD.a 0 //AD.b 4 //AD.c 24 //AD sizeof 32 // //A1.a 0 //A1.b 4 //A1.c 17 //A1 sizeof 25 // //A2.a 0 //A2.b 4 //A2.c 18 //A2 sizeof 26 // //A4.a 0 //A4.b 4 //A4.c 20 //A4 sizeof 28 // //A8.a 0 //A8.b 4 //A8.c 24 //A8 sizeof 32 // //A16.a 0 //A16.b 4 //A16.c 24 //A16 sizeof 32 // //

user: user-3
content: 结构体对齐，按照结构体元素中占用字节最大的成员标准对齐。int，4字节，即按照4字节对齐

user: user-4
content: http://www.cppblog.com/snailcong/archive/2009/03/16/76705.html 这个贴子总结得比较好.

user: user-5
content: 引用 1 楼 T20091 的回复:short是二字节对齐，int是四字节对齐  struct data1 {         char a;         short b;         char c;         int d; }; data1：a一字节|空一个字节|b两个字节|c一字节|空三个字节|d四字节  struct data2 {         char a;         char b;         short c;         int d; }; data2：a一字节|b一字节|c两字节|d四字节 引用 2 楼 user-8 的回复:每个变量相对于起始位置的偏移量必须是该变量类型大小的整数倍，不是整数倍空出内存，直到偏移量是整数倍为止。  最后整个结构体的大小必须是里面变量类型最大值的整数倍。  一楼分析的很全面，二楼总结到点上了

user: user-6
content: short是二字节对齐，int是四字节对齐  struct data1 {         char a;         short b;         char c;         int d; }; data1：a一字节|空一个字节|b两个字节|c一字节|空三个字节|d四字节  struct data2 {         char a;         char b;         short c;         int d; }; data2：a一字节|b一字节|c两字节|d四字节

user: user-7
content: 一楼说的很好，结构体data1，因为内存对齐，会自动补充，第二个是自动对齐，没有补充。   #include<stdio.h>  struct data1 { 	char a; 	char x1; 	short b; 	char c; 	char x2; 	char x3; 	char x4; 	int d; };  struct data2 { 	char a; 	char b; 	short c; 	int d; }; void main() { 	printf("%d\n",sizeof(data1)); 	printf("%d\n",sizeof(data2)); }

user: user-8
content: 每个变量相对于起始位置的偏移量必须是该变量类型大小的整数倍，不是整数倍空出内存，直到偏移量是整数倍为止。  最后整个结构体的大小必须是里面变量类型最大值的整数倍。