sizeof(class) 转载,偶的笔记

原创 2007年09月23日 22:17:00

Determining the Size of a Class Object

By Girish Shetty

There are many factors that decide the size of an object of a class in C++. These factors are:
  1. Size of all non-static data members
  2. Order of data members
  3. Byte alignment or byte padding
  4. Size of its immediate base class
  5. The existence of virtual function(s) (Dynamic polymorphism using virtual functions).
  6. Compiler being used
  7. Mode of inheritance (virtual inheritance)


<script type="text/javascript"><!-- google_ad_client = "pub-2560316224908115"; google_ad_width = 336; google_ad_height = 280; google_ad_format = "336x280_as"; google_ad_type = "text_image"; google_ad_channel ="4570121886"; google_color_border = "FFFFFF"; google_color_bg = "FFFFFF"; google_color_link = "C30000"; google_color_url = "C30000"; google_color_text = "000000"; //--></script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script> <iframe width="336" scrolling="no" height="280" frameborder="0" name="google_ads_frame" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-2560316224908115&amp;dt=1190556476875&amp;lmt=1163954482&amp;prev_fmts=120x90_0ads_al&amp;format=336x280_as&amp;output=html&amp;correlator=1190556476828&amp;channel=4570121886&amp;url=http%3A%2F%2Fwww.cprogramming.com%2Ftutorial%2Fsize_of_class_object.html&amp;color_bg=FFFFFF&amp;color_text=000000&amp;color_link=C30000&amp;color_url=C30000&amp;color_border=FFFFFF&amp;ad_type=text_image&amp;ref=http%3A%2F%2Fwww.google.com%2Fsearch%3Fsource%3Dig%26hl%3Den%26q%3Dthe%2Bsize%2Bof%2Bc%252B%252B%2Bclass%26btnG%3DGoogle%2BSearch&amp;cc=61&amp;ga_vid=1884489229.1190556477&amp;ga_sid=1190556477&amp;ga_hid=506384435&amp;flash=9&amp;u_h=768&amp;u_w=1024&amp;u_ah=715&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_his=1&amp;u_java=true&amp;u_nplug=16&amp;u_nmime=61" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true"></iframe>

Size of all non-static data members

Only non-static data members will be counted for calculating sizeof class/object.
class A { 
private:
float iMem1;
const int iMem2;
static int iMem3;
char iMem4;
};
For an object of class A, the size will be the size of float iMem1 + size of int iMem2 + size of char iMem3. Static members are really not part of the class object. They won't be included in object's layout. <2>Order of data members The order in which one specifies data members also alters the size of the class.
class C { 
char c;
int int1;
int int2;
int i;
long l;
short s;
};
The size of this class is 24 bytes. Even though char c will consume only 1 byte, 4 bytes will be allocated for it, and the remaining 3 bytes will be wasted (holes). This is because the next member is an int, which takes 4 bytes. If we don't go to the next (4th) byte for storing this integer member, the memory access/modify cycle for this integer will be 2 read cycles. So the compiler will do this for us, unless we specify some byte padding/packing.

If I re-write the above class in different order, keeping all my data members like below:
class C { 
int int1;
int int2;
int i;
long l;
short s;
char c;
};
Now the size of this class is 20 bytes.

In this case, it is storing c, the char, in one of the slots in the hole in the extra four bytes.

Byte alignment or byte padding

As mentioned above, if we specify 1 byte alignment, the size of the class above (class C) will be 19 in both cases.

Size of its immediate base class

The size of a class also includes size of its immediate base class.

Lets take an example:
Class B { 
...
int iMem1;
int iMem2;
}

Class D: public B {
...
int iMem;
}
In this case, sizeof(D) is will also include the size of B. So it will be 12 bytes.

The existence of virtual function(s)

Existence of virtual function(s) will add 4 bytes of virtual table pointer in the class, which will be added to size of class. Again, in this case, if the base class of the class already has virtual function(s) either directly or through its base class, then this additional virtual function won't add anything to the size of the class. Virtual table pointer will be common across the class hierarchy. That is
class Base { 
public:
...
virtual void SomeFunction(...);
private:
int iAMem
};

class Derived : public Base {
...
virtual void SomeOtherFunction(...);
private:
int iBMem
};
In the example above, sizeof(Base) will be 8 bytes--that is sizeof(int iAMem) + sizeof(vptr). sizeof(Derived) will be 12 bytes, that is sizeof(int iBMem) + sizeof(Derived). Notice that the existence of virtual functions in class Derived won't add anything more. Now Derived will set the vptr to its own virtual function table.

Compiler being used

In some scenarios, the size of a class object can be compiler specific. Lets take one example:
class BaseClass { 
int a;
char c;
};

class DerivedClass : public BaseClass {
char d;
int i;
};
If compiled with the Microsoft C++ compiler, the size of DerivedClass is 16 bytes. If compiled with gcc (either c++ or g++), size of DerivedClass is 12 bytes.

The reason for sizeof(DerivedClass) being 16 bytes in MC++ is that it starts each class with a 4 byte aligned address so that accessing the member of that class will be easy (again, the memory read/write cycle).

Mode of inheritance (virtual inheritance)

In C++, sometimes we have to use virtual inheritance for some reasons. (One classic example is the implementation of final class in C++.) When we use virtual inheritance, there will be the overhead of 4 bytes for a virtual base class pointer in that class.
class ABase{ 
int iMem;
};

class BBase : public virtual ABase {
int iMem;
};

class CBase : public virtual ABase {
int iMem;
};

class ABCDerived : public BBase, public CBase {
int iMem;
};
And if you check the size of these classes, it will be:
  • Size of ABase : 4
  • Size of BBase : 12
  • Size of CBase : 12
  • Size of ABCDerived : 24
Because BBase and CBase are dervied from ABase virtually, they will also have an virtual base pointer. So, 4 bytes will be added to the size of the class (BBase and CBase). That is sizeof ABase + size of int + sizeof Virtual Base pointer.

Size of ABCDerived will be 24 (not 28 = sizeof (BBase + CBase + int member)) because it will maintain only one Virtual Base pointer (Same way of maintaining virtual table pointer).
<iframe width="336" scrolling="no" height="160" frameborder="0" name="ch_ad53" src="http://mm.chitika.net/minimall?w=336&amp;h=160&amp;client=cprogram&amp;noctxt=1&amp;sid=c++%20compiler&amp;url=http%3A//www.cprogramming.com/tutorial/size_of_class_object.html&amp;query=c++%20compiler&amp;nosearch=1&amp;cl_border=FFFFFF&amp;cl_title=C30000&amp;cl_text=000000&amp;ref=http%3A//www.google.com/search%3Fsource%3Dig%26hl%3Den%26q%3Dthe+size+of+c%252B%252B+class%26btnG%3DGoogle+Search&amp;cb=53" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true"></iframe>
 

【转载】[学习笔记]c sizeof strlen的区别

【转载】[学习笔记]c sizeof strlen的区别 出自:http://zouyou1986.blog.163.com/blog/static/1772625920097132525510...

关于sizeof(Class)的学习

#include #include using namespace std; class Base { public: //每个自动成员变量占用一定的空间 ...
  • s_a_n_
  • s_a_n_
  • 2013年03月05日 20:24
  • 1103

class的sizeof

#include class a {}; class b{}; class c:public a{ virtual void fun()=0; }; ...
  • zyyoung
  • zyyoung
  • 2011年01月27日 16:59
  • 5463

C++中struct/class的数据对齐与sizeof

一、什么是对齐,以及为什么要对齐 1. 现代计算机中内存空间都是按照byte划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定变量的时候经常在特定的内存地址访问,这...
  • gswjj1
  • gswjj1
  • 2013年03月29日 20:16
  • 533

C++类的大小——sizeof(class)

第一:空类的大小class CBase { }; 运行cout

c/c++中使用sizeof计算结构体struct和类class的长度(字节)

前言 在默认情况下,为了方便对结构体内元素的访问和管理,当结构体内的元素长度都小于处理器的位数的时候,便以结构体内最长的数据元素为对齐单位,也就是说,结构体的长度一定是最长的数据元素的整数倍。如果结构...
  • hsd2012
  • hsd2012
  • 2016年03月24日 09:44
  • 2423

class sizeof

Determining the Size of a Class Object By Girish Shetty There are many factors that decide the...

sizeof class 详解(类的大小问题实验剖析)

类的大小问题实验剖析: 1、空类:C++编译器强制给这种类插入一个缺省成员,长度为1。如果有自定义的变量,变量将取代这个缺省成员。 class A {}; cout输出1 2...

《C++笔记》 Part6 浅析extern的作用比较分析与数组相关的sizeof和strlen

《C++笔记》 Part5 浅析extern的作用比较分析与数组相关的sizeof和strlen 转自:http://www.cnblogs.com/Lynn-Zhang/p/5383201....

程序员面试精要-C/C++程序设计-预处理/const/sizeof(面试宝典读书笔记)

宏定义 1.宏定义展开容易造成二义性问题 #define SQR(x) (x*x) a =SQR(b+2)展开后为b+2*b+2而不是”(b+2)*(b+2)” 若要平方形式对表达式适用,可以...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:sizeof(class) 转载,偶的笔记
举报原因:
原因补充:

(最多只允许输入30个字)