标签:
本文记录了有关sizeof的一些计算,主要有下面的四种情况:(如有错误,敬请留言)
使用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; }
标签:
原文地址:http://www.cnblogs.com/zi-xing/p/4593835.html