From:http://blog.youkuaiyun.com/cg05568256068/archive/2009/04/17/4086451.aspx
内存地址的对齐主要考虑三个因素:
1:对于每个成员的起始地址是他本身所占的整数倍
2:整个所占的内存是成员中占的地址内存最多的整数倍
3:有#pragma pack(int)进行设置,如果结构体某成员的sizeof大于你设置的,则按你的设置来对齐
注意:每次用#pragma pack(int)进行设置后,要用#pragma pack()对其结束,免得造成错误
- #include<iostream>
- usingnamespacestd;
- structA
- {
- inta;//0123
- charb;//4*
- shortc;//67
- };
- structB
- {
- chara;//0***
- intb;//4567
- shortc;//89**
- };
- #pragmapack(2)
- structC
- {
- chara;//0*
- intb;//2345
- shortc;//67
- };
- #pragmapack()
- #pragmapack(1)
- structD
- {
- chara;//0
- intb;//1234
- shortc;//56
- };
- #pragmapack()
- intmain()
- {
- cout<<"sizeof(A)="<<sizeof(A)<<endl;
- cout<<"sizeof(B)="<<sizeof(B)<<endl;
- cout<<"sizeof(C)="<<sizeof(C)<<endl;
- cout<<"sizeof(D)="<<sizeof(D)<<endl;
- return0;
- }
#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; }
- sizeof(A)=8
- sizeof(B)=12
- sizeof(C)=8
- sizeof(D)=7
- Pressanykeytocontinue
sizeof(A) = 8 sizeof(B) = 12 sizeof(C) = 8 sizeof(D) = 7 Press any key to continue
注意下面我对#pragma pack() 用法的理解,他不仅对每个成员的起始地址起作用,对整个的也起作用。请看下面的测试程序
- #include<iostream>
- usingnamespacestd;
- #pragmapack(4)
- structAA
- {
- chara;//0*******
- doubleb;//4567891011
- intc;//1213**
- };//16
- #pragmapack()
- #pragmapack(2)
- structAAA
- {
- chara;//0*
- doubleb;//23456789起始地址也只要是2的倍数,不是8的倍数
- intc;//1011**
- };//14只要保证是2的倍数,不是最大double的8的倍数
- #pragmapack()
- intmain()
- {
- cout<<"sizeof(AA)="<<sizeof(AA)<<endl;
- cout<<"sizeof(AAA)="<<sizeof(AAA)<<endl;
- return0;
- }
#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; }
- sizeof(A)=24
- sizeof(AA)=40
- sizeof(A2)=20
- sizeof(AA2)=28
- Pressanykeytocontinue
sizeof(A) = 24 sizeof(AA) = 40 sizeof(A2) = 20 sizeof(AA2) = 28 Press any key to continue
下面接受有结构体嵌套的时候:
对于整个结构体作为成员函数时,他的起始地址不是以该结构体整体作为标准,而是以其内的成员为标准
- #include<iostream>
- usingnamespacestd;
- structA
- {
- inta;//0123
- charb;//4***
- doublec;//89101112131415
- shortd;//1617******
- };//24
- structAA
- {
- chara;//0*******
- Ab;//8..31
- intc;//32333435****
- };
- #pragmapack(4)
- structA2
- {
- inta;//0123
- charb;//4***
- doublec;//89101112131415
- shortd;//1617**
- };//20
- #pragmapack()
- structAA2
- {
- chara;//0***
- A2b;//4..23
- intc;//24252627
- };
- intmain()
- {
- cout<<"sizeof(A)="<<sizeof(A)<<endl;
- cout<<"sizeof(AA)="<<sizeof(AA)<<endl;
- cout<<"sizeof(A2)="<<sizeof(A2)<<endl;
- cout<<"sizeof(AA2)="<<sizeof(AA2)<<endl;
- return0;
- }
#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; }
- sizeof(A)=24
- sizeof(AA)=16
- sizeof(AAA)=14
- Pressanykeytocontinue
sizeof(A) = 24 sizeof(AA) = 16 sizeof(AAA) = 14 Press any key to continue
最后注意:static成员和函数其实是类层次的,不在对象中分配空间,而成员函数其实是被编译为全局函数了,所以也不在对象中。
- #include<iostream>
- usingnamespacestd;
- structempty{};//1
- structconstAndStatic
- {
- constinti;//0123****
- constdoubled;//89101112131415
- staticcharc;
- staticvoidTestStatic(){}
- voidTestNoStatic(){}
- };//16
- intmain()
- {
- cout<<"sizeof(empty)="<<sizeof(empty)<<endl;
- cout<<"sizeof(constAndStatic)="<<sizeof(constAndStatic)<<endl;
- return0;
- }
#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; }
- sizeof(empty)=1
- sizeof(constAndStatic)=16
- Pressanykeytocontinue
1946

被折叠的 条评论
为什么被折叠?



