天天看點

sizeof(struct)及sizeof(class)

http://community.csdn.net/Expert/topic/4405/4405757.xml?temp=9.743899E-02

#param package(1)

struct a

{

    __int32 i;

    __int8 c[5];

    double* p;

    __int16 j;

};

printf("sizeof(struct a)");

這是多少?

------------------------

using namespace std;

class Base

{

public:

    Base() { cout<<"Base-ctor"<<endl; }

    ~Base() { cout<<"Base-dtor"<<endl; }

    virtual void f(int) { cout<<"Base::f(int)"<<endl; }

    virtual void f(double) {cout<<"Base::f(double)"<<endl; }

    virtual void g(int i = 10) {cout<<"Base::g()"<<i<<endl; }

};

這裡sizeof(Base)的話又是多少?

------------------------------

answer:

第一個結構 是不對齊方式

大小= 4+5+4+2 = 15

第二個無成員但有vptr指針為4

如果無virtual 應該為1