RE: sizeof empty class

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Ranjith,

> What is the size of an an empty class?

Zero, plus fallow padding byte(s), since an instance of the class needs to have an address.

> On my machine it is showing 1.

I would expect sizeof(EmptyClass) to be either the same as sizeof(char) or sizeof(int).

> What is stored at that 1 byte memory?

Garbage.

> will sizeof() function ever return zero?

The sizeof keyword includes the fallow padding bytes.

> How constructor/destructor are called when an object is created?

When the object is created (new, or on the stack, or as a global initializer), the constructor is called.

If the constructor is not provided in your code, the compiler will synthesize an inline one.  If the synthesized constructor has nothing to do the optimizer may remove it entirely.

When the object is deleted (delete, end of scope, or as a global terminator) , the destructor is called.

If the destructor is not provided in your code, the compiler will synthesize an inline one.  If the synthesized destructor has nothing to do the optimizer may remove it entirely.

> Any hidden variables will be created by the compiler inside the class?

If the class has a virtual function table, the virtual function table pointer will be initialized.

> Why destructor of derived class is not called when we delete a baseclass pointer which is pointing to derived class?

Does the base class have a virtual destructor?  A virtual destructor is *required* to be able to delete a derived class through a base class pointer.

HTH,
--Eljay


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux