您的位置:程序门 -> c/c++ -> c++ 语言



关于sizeof


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


关于sizeof[已结贴,结贴人:liumyua]
发表于:2008-01-21 18:42:46 楼主
#include   "stdio.h"
void   main(void)
{
              class   a   {};
              class   b   :   virtual   public   a   {};
              class   c:   public   a   {};
              class   d:   public   b,   public   c   {};
              class   e:   virtual   public   b,   virtual   public   c   {};  
              printf("sizeof(a)   =   %d\n",sizeof(a));
              printf("sizeof(b)   =   %d\n",sizeof(b));
              printf("sizeof(c)   =   %d\n",sizeof(c));
              printf("sizeof(d)   =   %d\n",sizeof(d));
              printf("sizeof(e)   =   %d\n",sizeof(e));
}

///////////////////
sizeof(a)   =   1
sizeof(b)   =   4
sizeof(c)   =   1
sizeof(d)   =   8
sizeof(e)   =   12
press   any   key   to   continue
(vc下)
听说是笔试题
看不懂   哪位高手帮帮忙解释下
发表于:2008-01-21 18:51:261楼 得分:20
一个空类并不是真空的,里面有一字节填充,以表示该类可以实例化.所以sizeof(a)=1
有virtual关键字的类,由编译器生成一个vptr,所以sizeof(b)=4
建议楼主看看inside   c++   object   model.关于c++对象的内存分布.
发表于:2008-01-21 19:11:202楼 得分:0
sizeof(a)       =       1
sizeof(b)       =       4
sizeof(c)       =       1
sizeof(d)       =       8
sizeof(e)       =       8
请按任意键继续.   .   .
dev   c++   下
发表于:2008-01-21 19:18:233楼 得分:0
多谢dchilli   指点
有virtual关键字的类,由编译器生成一个vptr,所以sizeof(b)=4  
那么继承是怎么分配的?
发表于:2008-01-21 19:44:064楼 得分:10
那么继承是怎么分配的?

??

你是说内存布局吧?

不同的编译器有不同的实现,vc一般是把虚表放前面,接着是基类的数据,然后是自己的
发表于:2008-01-21 21:45:145楼 得分:50
class   x   {};  
in   practice   is   never   empty.   rather   it   has   an   associated   size   of   1   byte梐   char   member   inserted   by   the   compiler.   this   allows   two   objects   of   the   class,   such   as
x   a,   b;  
if   (   &a   ==   &b   )   cerr   < <   "yipes!"   < <   endl;  
to   be   allocated   unique   addresses   in   memory.

class   y   :   public   virtual   x{};  
class   z   :   public   virtual   x{};  
this   size,   however,   is   partially   machine   dependent.   it   also   depends   in   part   on   the   compiler   implementation   being   used.   the   given   size   of   both   class   y   and   class   z   on   any   machine   is   the   interplay   of   three   factors:
1.             language   support   overhead.  
there   is   an   associated   overhead   incurred   in   the   language   support   of   virtual   base   classes.   within   the   derived   class,   this   overhead   is   reflected   as   some   form   of   pointer,   either   to   the   virtual   base   class   subobject   or   to   an   associated   table   within   which   either   the   address   or   offset   to   the   virtual   base   class   subobject   is   stored.   on   my   correspondent's   machine,   the   pointer   is   4   bytes.

2.             compiler   optimization   of   recognized   special   cases.  
  there   is   the   1   byte   size   of   the   virtual   base   class   x   subobject   also   present   within   y   (and   z).   traditionally,   this   is   placed   at   the   end   of   the   "fixed"   (that   is,   invariant)   portion   of   the   derived   class.   some   compilers   now   provide   special   support   for   an   empty   virtual   base   class   (the   paragraph   following   item   3   discusses   this   in   more   detail).   our   correspondent's   compiler,   however,   did   not   provide   this   special   handling.

3.             alignment   constraints.
the   size   of   class   y   (and   z)   at   this   point   is   5   bytes.   on   most   machines,   aggregate   structures   have   an   alignment   constraint   so   that   they   can   be   efficiently   loaded   from   and   stored   to   memory.   on   my   correspondent's   machine,   alignment   of   an   aggregate   is   on   a   4-byte   boundary.   so   class   y   (and   z)   requires   3   bytes   of   padding.   the   result   is   a   final   size   of   8.


发表于:2008-01-21 22:10:226楼 得分:20
http://blog.programfan.com/article.asp?id=30210
发表于:2008-01-21 22:21:587楼 得分:20
class   b由于是虚拟集成class   a的,就想chiyer   所说.class   b里面应该包涵指向了class   a的指针,然后是a的数据和自己的数据.由于a的数据和b的数据为0.所以class   b就只剩下那个指向class   a指针.所以sizeof(b)=4;

而class   c并不是虚拟继承,所以就不需要一个指向base   class的指针了.但是它要可以由编译器识别出来,所以编译器就有了一个一字节的识别码.于是就有了sizeof(c)=1

而sizeof(d)=sizeof(b)+sizeof(c)+3字节的padding,所以就等于8了.

应该就是这样了.楼主看看inside   c++   model吧,很好的一本书.
发表于:2008-01-21 23:51:088楼 得分:0
最后那个纯属折腾人,前面的几个知道概念都能说出来
发表于:2008-01-22 18:22:589楼 得分:0
sizeof(*)   求数据类型占用内存空间!
发表于:2008-01-22 20:59:2210楼 得分:0
终于看懂了  
  class       a       {};  
                            class       b       :       virtual       public       a       {};  
                            class       c:       public       a       {};  
                            class       d:       public       b,       public       c       {};  
                            class       e:       virtual       public       b,       virtual       public       c       {};      
sizeof(a)           //由于a为空   但需要留一位作为标识
sizeof(b)           //为a的大小   +   4     (vptr的大小   不同编译器可能不样)
sizeof(c)           //   为a的大小+自身大小  
sizeof(d)           //   b的大小+c的大小   (bc中若有一个为virtual,则非virtual需要内存对其)
sizeof(e)           //d的大小+4    


快速检索

最新资讯
热门点击