有了前面幾節的鋪墊,本節開始摸索C++的對象的內存佈局,平臺爲windows32位+VS2008。
一 內置類型的size
內置類型,直接上代碼,幫助大家加深記憶:
void TestBasicSizeOf()
{
cout << __FUNCTION__ << endl;
cout << " sizeof(char)= " << sizeof ( char ) << endl;
cout << " sizeof(int)= " << sizeof ( int ) << endl;
cout << " sizeof(float)= " << sizeof ( float ) << endl;
cout << " sizeof(double)= " << sizeof ( double ) << endl;
cout << " sizeof('$')=" << sizeof ( '$' ) << endl;
cout << " sizeof(1)= " << sizeof ( 1 ) << endl;
cout << " sizeof(1.5f)= " << sizeof ( 1.5f ) << endl;
cout << " sizeof(1.5)= " << sizeof ( 1.5 ) << endl;
cout << " sizeof(Good!)= " << sizeof ( "Good!" ) << endl ;
char str[] = "CharArray!";
int a[10];
double xy[10];
cout << " char str[] = /"CharArray!/"," << " sizeof(str)= " << sizeof (str) << endl;
cout << " int a[10]," << " sizeof(a)= " << sizeof (a) << endl;
cout << " double xy[10]," << " sizeof(xy)= " << sizeof (xy) << endl;
cout << " sizeof(void*)= " << sizeof(void*) << endl;
}
運行結果如下:
二 struct/class的大小
在C++中我們知道struct和class的唯一區別就是默認的訪問級別不同,struct默認爲public,而class的默認爲private。所以考慮對象的大小,我們均以struct爲例。對於struct的大小對於初學者來說還確實是個難回答的問題,我們就通過下面的一個struct定義加逐步的變化來引出相關的知識。
代碼如下:
Code
struct st1
{
short number;
float math_grade;
float Chinese_grade;
float sum_grade;
char level;
}; //20
struct st2
{
char level;
short number;
float math_grade;
float Chinese_grade;
float sum_grade;
};//16
#pragma pack(1)
struct st3
{
char level;
short number;
float math_grade;
float Chinese_grade;
float sum_grade;
}; //15
#pragma pack()
void TestStructSizeOf()
{
cout << __FUNCTION__ << endl;
cout << " sizeof(st1)= " << sizeof (st1) << endl;
cout << " offsetof(st1,number) " << offsetof(st1,number) << endl;
cout << " offsetof(st1,math_grade) " << offsetof(st1,math_grade) << endl;
cout << " offsetof(st1,Chinese_grade) " << offsetof(st1,Chinese_grade) << endl;
cout << " offsetof(st1,sum_grade) " << offsetof(st1,sum_grade) << endl;
cout << " offsetof(st1,level) " << offsetof(st1,level) << endl;
cout << " sizeof(st2)= " << sizeof (st2) << endl;
cout << " offsetof(st2,level) " << offsetof(st2,level) << endl;
cout << " offsetof(st2,number) " << offsetof(st2,number) << endl;
cout << " offsetof(st2,math_grade) " << offsetof(st2,math_grade) << endl;
cout << " offsetof(st2,Chinese_grade) " << offsetof(st2,Chinese_grade) << endl;
cout << " offsetof(st2,sum_grade) " << offsetof(st2,sum_grade) << endl;
cout << " sizeof(st3)= " << sizeof (st3) << endl;
cout << " offsetof(st3,level) " << offsetof(st3,level) << endl;
cout << " offsetof(st3,number) " << offsetof(st3,number) << endl;
cout << " offsetof(st3,math_grade) " << offsetof(st3,math_grade) << endl;
cout << " offsetof(st3,Chinese_grade) " << offsetof(st3,Chinese_grade) << endl;
cout << " offsetof(st3,sum_grade) " << offsetof(st3,sum_grade) << endl;
}
運行結果如下;
基於上面的對struct的測試,我們是不是有些驚呆哦,對於C++的初學者更是情不自禁的說:“我靠!原來順序不同所佔空間都不同啊,還有那個pack是啥東東啊?”,其實這裏蘊含了一個內存對齊的問題,在計算機的底層進行內存的讀寫的時候,如果內存對齊的話可以提高讀寫效率,下面是VC的默認規則:
1) 結構體變量的首地址能夠被其最寬基本類型成員的大小所整除;
2) 結構體每個成員相對於結構體首地址的偏移量(offset)都是成員大小的整數倍, 如有需要編譯器會在成員之間加上填充字節(internal adding);
3) 結構體的總大小爲結構體最寬基本類型成員大小的整數倍,如有需要編譯器會在最末一個成員之後加上填充字節(trailing padding)。
當然VC提供了工程選項/Zp[1|2|4|8|16]可以修改對齊方式,當然我們也可以在代碼中對部分類型實行特殊的內存對齊方式,修改方式爲#pragma pack( n ),n爲字節對齊
數,其取值爲1、2、4、8、16,默認是8,取消修改用#pragma pack(),如果結構體某成員的sizeof大於你設置的,則按你的設置來對齊。
三 struct的嵌套
1)實例:
Code
struct A
{
int i;
char c;
double d;
short s;
}; // 24
struct B
{
char cc;
A a;
int ii;
}; // 40
佈局:(使用VS的未發佈的編譯選項/d1 reportAllClassLayout 或 /d1 reportSingleClassLayout)
2)實例:
Code
#pragma pack(4)
struct A2
{
int i;
char c;
double d;
short s;
}; // 20
#pragma pack()
struct B2
{
char cc;
A2 a;
int ii;
}; // 28
佈局:(使用VS的未發佈的編譯選項/d1 reportAllClassLayout 或 /d1 reportSingleClassLayout)
總結:
由於結構體的成員可以是複合類型,比如另外一個結構體,所以在尋找最寬基本類型成員時,應當包括複合類型成員的子成員,而不是把複合成員看成是一個整體。但在確定複合類型成員的偏移位置時則是將複合類型作爲整體看待。
四 空struct/class和const,static成員
實例:
Code
struct empty{}; // 1
struct constAndStatic
{
const int i;
static char c;
const double d;
static void TestStatic(){}
void TestNoStatic(){}
}; // 16
佈局:(使用VS的未發佈的編譯選項/d1 reportAllClassLayout 或 /d1 reportSingleClassLayout)
上面的實例中empty的大小爲1,而constAndStatic的大小爲16。
總結:
因爲static成員和函數其實是類層次的,不在對象中分配空間,而成員函數其實是被編譯爲全局函數了,所以也不在對象中。
五 本節完,下次探討虛函數對內存佈局的影響!
感謝,Thanks!
作者:iTech
出處:http://itech.cnblogs.com/
轉載:本文版權歸作者iTech所有,轉載請註明出處,不得用於商業用途!