内存地址的对齐

From:http://blog.youkuaiyun.com/cg05568256068/archive/2009/04/17/4086451.aspx

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

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

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

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

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

  1. #include<iostream>
  2. usingnamespacestd;
  3. structA
  4. {
  5. inta;//0123
  6. charb;//4*
  7. shortc;//67
  8. };
  9. structB
  10. {
  11. chara;//0***
  12. intb;//4567
  13. shortc;//89**
  14. };
  15. #pragmapack(2)
  16. structC
  17. {
  18. chara;//0*
  19. intb;//2345
  20. shortc;//67
  21. };
  22. #pragmapack()
  23. #pragmapack(1)
  24. structD
  25. {
  26. chara;//0
  27. intb;//1234
  28. shortc;//56
  29. };
  30. #pragmapack()
  31. intmain()
  32. {
  33. cout<<"sizeof(A)="<<sizeof(A)<<endl;
  34. cout<<"sizeof(B)="<<sizeof(B)<<endl;
  35. cout<<"sizeof(C)="<<sizeof(C)<<endl;
  36. cout<<"sizeof(D)="<<sizeof(D)<<endl;
  37. return0;
  38. }

#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. Pressanykeytocontinue

sizeof(A) = 8 sizeof(B) = 12 sizeof(C) = 8 sizeof(D) = 7 Press any key to continue

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

  1. #include<iostream>
  2. usingnamespacestd;
  3. #pragmapack(4)
  4. structAA
  5. {
  6. chara;//0*******
  7. doubleb;//4567891011
  8. intc;//1213**
  9. };//16
  10. #pragmapack()
  11. #pragmapack(2)
  12. structAAA
  13. {
  14. chara;//0*
  15. doubleb;//23456789起始地址也只要是2的倍数,不是8的倍数
  16. intc;//1011**
  17. };//14只要保证是2的倍数,不是最大double的8的倍数
  18. #pragmapack()
  19. intmain()
  20. {
  21. cout<<"sizeof(AA)="<<sizeof(AA)<<endl;
  22. cout<<"sizeof(AAA)="<<sizeof(AAA)<<endl;
  23. return0;
  24. }

#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. Pressanykeytocontinue

sizeof(A) = 24 sizeof(AA) = 40 sizeof(A2) = 20 sizeof(AA2) = 28 Press any key to continue

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

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

  1. #include<iostream>
  2. usingnamespacestd;
  3. structA
  4. {
  5. inta;//0123
  6. charb;//4***
  7. doublec;//89101112131415
  8. shortd;//1617******
  9. };//24
  10. structAA
  11. {
  12. chara;//0*******
  13. Ab;//8..31
  14. intc;//32333435****
  15. };
  16. #pragmapack(4)
  17. structA2
  18. {
  19. inta;//0123
  20. charb;//4***
  21. doublec;//89101112131415
  22. shortd;//1617**
  23. };//20
  24. #pragmapack()
  25. structAA2
  26. {
  27. chara;//0***
  28. A2b;//4..23
  29. intc;//24252627
  30. };
  31. intmain()
  32. {
  33. cout<<"sizeof(A)="<<sizeof(A)<<endl;
  34. cout<<"sizeof(AA)="<<sizeof(AA)<<endl;
  35. cout<<"sizeof(A2)="<<sizeof(A2)<<endl;
  36. cout<<"sizeof(AA2)="<<sizeof(AA2)<<endl;
  37. return0;
  38. }

#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. Pressanykeytocontinue

sizeof(A) = 24 sizeof(AA) = 16 sizeof(AAA) = 14 Press any key to continue

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

  1. #include<iostream>
  2. usingnamespacestd;
  3. structempty{};//1
  4. structconstAndStatic
  5. {
  6. constinti;//0123****
  7. constdoubled;//89101112131415
  8. staticcharc;
  9. staticvoidTestStatic(){}
  10. voidTestNoStatic(){}
  11. };//16
  12. intmain()
  13. {
  14. cout<<"sizeof(empty)="<<sizeof(empty)<<endl;
  15. cout<<"sizeof(constAndStatic)="<<sizeof(constAndStatic)<<endl;
  16. return0;
  17. }

#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. Pressanykeytocontinue
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值