首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

关于sizeof解决办法

2012-02-17 
关于sizeof#include stdio.hvoid main(void){class A {}class B : virtual public A {}class C: publi

关于sizeof
#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下)
听说是笔试题
看不懂 哪位高手帮帮忙解释下

[解决办法]
一个空类并不是真空的,里面有一字节填充,以表示该类可以实例化.所以sizeof(A)=1
有virtual关键字的类,由编译器生成一个vptr,所以sizeof(B)=4
建议楼主看看inside c++ object model.关于C++对象的内存分布.
[解决办法]
那么继承是怎么分配的?

??

你是说内存布局吧?

不同的编译器有不同的实现,vc一般是把虚表放前面,接着是基类的数据,然后是自己的
[解决办法]
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.




[解决办法]
http://blog.programfan.com/article.asp?id=30210
[解决办法]
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吧,很好的一本书.

热点排行