本文記錄了有關sizeof的一些計算,主要有下面的四種情況:(如有錯誤,敬請留言)
#include <iostream> #include <stdlib.h> using namespace std; //如果數組變量被傳入函數中做sizeof()運算,則和指針的運算沒有區別。 void Func( char str[100] ) { cout<<"Fun: == "<<sizeof(str)<<endl;//4字節的指針 } int main() { char str[] = "Hello"; char *p = str; int n = 10; cout<<"str == "<<sizeof(str)<<endl; //結果=6=strlen("Hello")+1;數值中要留一個元素保存字符串結束符 Func(str); //2個結果都是4,win32下,指針和int都是4字節 cout << "p ==" << sizeof(p) << endl; cout << "n ==" << sizeof(n) << endl;//結果4 void *q = malloc(100);//100字節的堆內存 cout << "void * == " << sizeof(q) << endl; //還是4(指針類型的大小) char *ch = (char *)malloc(sizeof(char)*10); cout << "char * == " <<sizeof(ch) << endl; //還是4(指針類型的大小) return 0; } //數組和指針的sizeof()是有區別的。 //對於指針,不論何種類型,其大小都是4字節的。
通過上面的示例,我們可以看到:sizeof計算的都是數據類型占用空間的大小,除了一種情況,就是初始化char str[]="Hello"的時候,sizeof計算的是字符串的長度+1.
sizeof計算類對象所占用空間的大小-用到了字節對齊
#include <iostream> using namespace std; class A { public: int i;//4 }; class AA { public: double d; int i;//8+4+4(補全) }; class B { public: char ch;//1 }; class C { public: int i; short j;//4+2+2(補全) }; class D { public: int i; short j; char ch;//4+2+1+1(補全) }; class E { public: int i; int ii; short l; char ch; char chr;//4+4+2+1+1 }; class F { public: int i; int ii; int iii; short j; char ch; char chr;//4+4+4+2+1+1 }; class FF { public: short j; int i; char ch; int ii; char chr; int iii;//2+2(補全)+4+1+3(補全)+4+1+3(補全)+4=24 }; int main() { cout<<"sizeof(double) == "<<sizeof(double)<<endl;//4字節 cout<<"sizeof(float) == "<<sizeof(float)<<endl;//4字節 cout<<"sizeof(int) == "<<sizeof(int)<<endl;//4字節 cout<<"sizeof(short) == "<<sizeof(short)<<endl;//2 cout<<"sizeof(char) == "<<sizeof(char)<<endl<<endl;//1 cout<<"sizeof(A) == "<<sizeof(A)<<endl; cout<<"sizeof(AA) == "<<sizeof(AA)<<endl;//最寬的類型是8字節的。 cout<<"sizeof(B) == "<<sizeof(B)<<endl; cout<<"sizeof(C) == "<<sizeof(C)<<endl; cout<<"sizeof(D) == "<<sizeof(D)<<endl; cout<<"sizeof(E) == "<<sizeof(E)<<endl; cout<<"sizeof(F) == "<<sizeof(F)<<endl; cout<<"sizeof(FF) == "<<sizeof(FF)<<endl;//同樣的類型個數,只是順序變了,就不一樣了。 return 0; }
sixeof計算含有虛函數的類對象的空間大小
#include <iostream> using namespace std; class Base { public: Base(int x):a(x){} void print() { cout<<"base"<<endl; } private: int a; }; class Dervied:public Base { public: Dervied(int x):Base(x-1),b(x){} void print() { cout<<"Dervied"<<endl; } private: int b; }; class A { public: A(int x):a(x){} virtual void print() { cout<<"A"<<endl; } private: int a; }; class B:public A { public: B(int x):A(x-1),b(x){} virtual void print() { cout<<"B"<<endl; } private: int b; }; int main() { Base obj1(1); cout<<"size of base obj1 is "<<sizeof(obj1)<<endl; Dervied obj2(2); cout<<"size of Dervied obj2 is "<<sizeof(obj2)<<endl; A a(1); cout<<"size of A obj is "<<sizeof(a)<<endl; B b(2); cout<<"size of B obj is "<<sizeof(b)<<endl; return 0; }
普通函數不占用內存,只要有虛函數,就會占用一個指針大小的內存,原因,系統多用了一個指針維護這個類的虛函數表, 並注意到,這個虛函數無論含有多少項(類中含有多少個虛函數)都不會再影響類的大小了。
sizeof計算虛擬繼承的類對象的空間大小
#include <iostream> using namespace std; class A{}; class B{}; class C:public A,public B{}; class D:virtual public A{}; class E:virtual public A,virtual public B{}; class F { public: int a; static int b; }; int F::b = 10; 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; cout<<"sizeof(E) =="<<sizeof(E)<<endl; cout<<"sizeof(F) =="<<sizeof(F)<<endl; return 0; }