This is the mail archive of the
mailing list for the GCC project.
RE: sizeof empty class
- From: "John (Eljay) Love-Jensen" <eljay at adobe dot com>
- To: ranjith kumar <ranjithproxy at gmail dot com>, "gcc-help at gcc dot gnu dot org" <gcc-help at gcc dot gnu dot org>
- Date: Thu, 8 Apr 2010 04:20:30 -0700
- Subject: RE: sizeof empty class
- References: <email@example.com>
> 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?
> 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.