1 #include <iostream> 2 using namespace std; 3 4 struct vpoet 5 { 6 int a; //4 bytes 7 char b; //1 bytes 8 double c; // 8 bytes 9 char *d; //4 bytes 10 int e; //4 bytes 11 }; 12 13 int main() 14 { 15 cout<<"sizeof(vpoet)="<<sizeof(vpoet)<<endl; 16 return 0; 17 }
vpoet中最大的是double double占8个字节,那么 编译器
的double最小整数倍来进行内存分配 。因而这里实际是在结构体
1 #pragma pack(n)
1 #include <iostream> 2 using namespace std; 3 #pragma pack(1) 4 5 struct vpoet 6 { 7 int a; //4 bytes 8 char b; //1 bytes 9 double c; // 8 bytes 10 char *d; //4 bytes 11 int e; //4 bytes 12 }; 13 14 int main() 15 { 16 cout<<"sizeof(vpoet)="<<sizeof(vpoet)<<endl; 17 return 0; 18 }
1 #pragma pack(2)