内存地址对齐 sizeof

 
内存地址对齐 sizeof
2010年05月11日 星期二 上午 08:48

内存地址的对齐主要考虑三个因素:

      1:对于每个成员的起始地址是他本身所占的整数倍

      2:整个所占的内存是成员中占的地址内存最多的整数倍

      3:有#pragma pack(int)进行设置,如 果结构体某成员的sizeof大于你设置的,则按你的设置来对齐

注意:每次用#pragma pack(int)进行设置后,要用#pragma pack()对其结束,免得造成错误

  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. struct A  
  5. {  
  6.     int a;        //0 1 2 3  
  7.     char b;       //4 *  
  8.     short c;      //6 7  
  9. };  
  10.   
  11. struct B  
  12. {  
  13.     char a;       //0 * * *  
  14.     int b;        //4 5 6 7  
  15.     short c;      //8 9 * *  
  16. };  
  17.   
  18. #pragma pack(2)  
  19. struct C  
  20. {  
  21.     char a;       //0 *  
  22.     int b;        //2 3 4 5  
  23.     short c;      //6 7  
  24. };  
  25. #pragma pack()  
  26.   
  27. #pragma pack(1)  
  28. struct D  
  29. {  
  30.     char a;       //0   
  31.     int b;        //1 2 3 4  
  32.     short c;      //5 6  
  33. };  
  34. #pragma pack()  
  35.   
  36. int main()  
  37. {  
  38.     cout<<"sizeof(A) = "<<sizeof(A)<<endl;  
  39.     cout<<"sizeof(B) = "<<sizeof(B)<<endl;  
  40.     cout<<"sizeof(C) = "<<sizeof(C)<<endl;  
  41.     cout<<"sizeof(D) = "<<sizeof(D)<<endl;  
  42.   
  43.     return 0;  
  44. }  
#include<iostream> using namespace std; struct A { int a; //0 1 2 3 char b; //4 * short c; //6 7 }; struct B { char a; //0 * * * int b; //4 5 6 7 short c; //8 9 * * }; #pragma pack(2) struct C { char a; //0 * int b; //2 3 4 5 short c; //6 7 }; #pragma pack() #pragma pack(1) struct D { char a; //0 int b; //1 2 3 4 short c; //5 6 }; #pragma pack() int main() { cout<<"sizeof(A) = "<<sizeof(A)<<endl; cout<<"sizeof(B) = "<<sizeof(B)<<endl; cout<<"sizeof(C) = "<<sizeof(C)<<endl; cout<<"sizeof(D) = "<<sizeof(D)<<endl; return 0; }

  1. sizeof(A) = 8  
  2. sizeof(B) = 12  
  3. sizeof(C) = 8  
  4. sizeof(D) = 7  
  5. Press any key to continue  
sizeof(A) = 8 sizeof(B) = 12 sizeof(C) = 8 sizeof(D) = 7 Press any key to continue

注意下面我对#pragma pack() 用法的理解,他不仅对每个成员的起始地址起作用,对整个的也起作用。请看下面的测试程序

  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. #pragma pack(4)  
  5. struct AA  
  6. {  
  7.     char a;      // 0 * * * * * * *  
  8.     double b;    //4 5 6 7 8 9 10 11  
  9.     int c;       //12 13 * *   
  10. };//16   
  11. #pragma pack()  
  12.   
  13. #pragma pack(2)  
  14. struct AAA  
  15. {  
  16.     char a;      // 0 *   
  17.     double b;    //2 3 4 5 6 7 8 9  起始地址也只要是2的倍数,不是8的倍数  
  18.     int c;       //10 11 * *   
  19. };//14 只要保证是2的倍数,不是最大double的8的倍数  
  20. #pragma pack()  
  21.   
  22. int main()  
  23. {  
  24.     cout<<"sizeof(AA) = "<<sizeof(AA)<<endl;  
  25.     cout<<"sizeof(AAA) = "<<sizeof(AAA)<<endl;  
  26.     return 0;  
  27. }  
#include<iostream> using namespace std; #pragma pack(4) struct AA { char a; // 0 * * * * * * * double b; //4 5 6 7 8 9 10 11 int c; //12 13 * * };//16 #pragma pack() #pragma pack(2) struct AAA { char a; // 0 * double b; //2 3 4 5 6 7 8 9 起始地址也只要是2的倍数,不是8的倍数 int c; //10 11 * * };//14 只要保证是2的倍数,不是最大double的8的倍数 #pragma pack() int main() { cout<<"sizeof(AA) = "<<sizeof(AA)<<endl; cout<<"sizeof(AAA) = "<<sizeof(AAA)<<endl; return 0; }

  1. sizeof(A) = 24  
  2. sizeof(AA) = 40  
  3. sizeof(A2) = 20  
  4. sizeof(AA2) = 28  
  5. Press any key to continue  
sizeof(A) = 24 sizeof(AA) = 40 sizeof(A2) = 20 sizeof(AA2) = 28 Press any key to continue

下面接受有结构体嵌套的时候:

      对于整个结构体作为成员函数时,他的起始地址不是以该结构体整体作为标准,而是以其内的成员为标准

  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. struct A  
  5. {  
  6.     int a;       //0 1 2 3  
  7.     char b;      //4 * * *   
  8.     double c;    //8 9 10 11 12 13 14 15  
  9.     short d;     //16 17 * * * * * *  
  10. };//24  
  11.   
  12. struct AA  
  13. {  
  14.     char a;      //0 * * * * * * *  
  15.     A b;         //8 .. 31  
  16.     int c;       //32 33 34 35 * * * *  
  17. };  
  18.   
  19. #pragma pack(4)  
  20. struct A2  
  21. {  
  22.     int a;       //0 1 2 3  
  23.     char b;      //4 * * *   
  24.     double c;    //8 9 10 11 12 13 14 15  
  25.     short d;     //16 17 * *   
  26. };//20  
  27. #pragma pack()  
  28.   
  29. struct AA2  
  30. {  
  31.     char a;      //0 * * *   
  32.     A2 b;         //4 .. 23  
  33.     int c;       //24 25 26 27  
  34. };  
  35.   
  36.   
  37.   
  38. int main()  
  39. {  
  40.   
  41.     cout<<"sizeof(A) = "<<sizeof(A)<<endl;  
  42.     cout<<"sizeof(AA) = "<<sizeof(AA)<<endl;  
  43.     cout<<"sizeof(A2) = "<<sizeof(A2)<<endl;  
  44.     cout<<"sizeof(AA2) = "<<sizeof(AA2)<<endl;  
  45.     return 0;  
  46. }  
#include<iostream> using namespace std; struct A { int a; //0 1 2 3 char b; //4 * * * double c; //8 9 10 11 12 13 14 15 short d; //16 17 * * * * * * };//24 struct AA { char a; //0 * * * * * * * A b; //8 .. 31 int c; //32 33 34 35 * * * * }; #pragma pack(4) struct A2 { int a; //0 1 2 3 char b; //4 * * * double c; //8 9 10 11 12 13 14 15 short d; //16 17 * * };//20 #pragma pack() struct AA2 { char a; //0 * * * A2 b; //4 .. 23 int c; //24 25 26 27 }; int main() { cout<<"sizeof(A) = "<<sizeof(A)<<endl; cout<<"sizeof(AA) = "<<sizeof(AA)<<endl; cout<<"sizeof(A2) = "<<sizeof(A2)<<endl; cout<<"sizeof(AA2) = "<<sizeof(AA2)<<endl; return 0; }

  1. sizeof(A) = 24  
  2. sizeof(AA) = 16  
  3. sizeof(AAA) = 14  
  4. Press any key to continue  
sizeof(A) = 24 sizeof(AA) = 16 sizeof(AAA) = 14 Press any key to continue

最后注意:static成员和函数其实是类层次的,不在对象中分配空 间,而成员函数其实是被编译为全局函数了,所以也不在对象中。

  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. struct empty{}; // 1  
  5. struct constAndStatic  
  6. {  
  7.     const int i;                 //0 1 2 3 * * * *  
  8.     const double d;              //8 9 10 11 12 13 14 15    
  9.     static char c;  
  10.     static void TestStatic(){}  
  11.     void TestNoStatic(){}  
  12. }; // 16  
  13.   
  14. int main()  
  15. {  
  16.   
  17.     cout<<"sizeof(empty) = "<<sizeof(empty)<<endl;  
  18.     cout<<"sizeof(constAndStatic) = "<<sizeof(constAndStatic)<<endl;  
  19.   
  20.     return 0;  
  21. }  
#include<iostream> using namespace std; struct empty{}; // 1 struct constAndStatic { const int i; //0 1 2 3 * * * * const double d; //8 9 10 11 12 13 14 15 static char c; static void TestStatic(){} void TestNoStatic(){} }; // 16 int main() { cout<<"sizeof(empty) = "<<sizeof(empty)<<endl; cout<<"sizeof(constAndStatic) = "<<sizeof(constAndStatic)<<endl; return 0; }

  1. sizeof(empty) = 1  
  2. sizeof(constAndStatic) = 16  
  3. Press any key to continue 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值