* RE: sizeof empty class
2010-04-08 9:01 sizeof empty class ranjith kumar
@ 2010-04-08 11:20 ` John (Eljay) Love-Jensen
2010-04-09 3:38 ` leon zadorin
2010-04-09 5:03 ` me22
2 siblings, 0 replies; 4+ messages in thread
From: John (Eljay) Love-Jensen @ 2010-04-08 11:20 UTC (permalink / raw)
To: ranjith kumar, gcc-help
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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: sizeof empty class
2010-04-08 9:01 sizeof empty class ranjith kumar
2010-04-08 11:20 ` John (Eljay) Love-Jensen
@ 2010-04-09 3:38 ` leon zadorin
2010-04-09 5:03 ` me22
2 siblings, 0 replies; 4+ messages in thread
From: leon zadorin @ 2010-04-09 3:38 UTC (permalink / raw)
To: ranjith kumar; +Cc: gcc-help
My understanding is that sizeof empty class/struct in C++ is defined
to never be 0 (every object must be addressable, e.g. in cases like
arrays and pointer arithmetic etc. etc.): "no object shall have the
same address in memory as any other variable"
in C(99) standard however, I am under impression but could be wrong,
the sizeof empty struct is undefined (i.e. could be 0):
C99: "If the struct-declaration-list contains no named members, the
behavior is undefined."
anyway -- just my 2 cents.
leon.
On 4/8/10, ranjith kumar <ranjithproxy@gmail.com> wrote:
> Hi,
>
> 1) What is the size of an an empty class? On my machine it is showing
> 1. What is stored at that 1 byte memory?
> 2) will sizeof() function ever return zero?
> 3)How constructor/destructor are called when an object is created?
> Any hidden virables will be crerated by the compiler inside the class?
> 4) Why destructor of derived class is not called when we delete a
> baseclass pointer which is pointing to derived class?
>
> Thansk in advance.
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: sizeof empty class
2010-04-08 9:01 sizeof empty class ranjith kumar
2010-04-08 11:20 ` John (Eljay) Love-Jensen
2010-04-09 3:38 ` leon zadorin
@ 2010-04-09 5:03 ` me22
2 siblings, 0 replies; 4+ messages in thread
From: me22 @ 2010-04-09 5:03 UTC (permalink / raw)
To: ranjith kumar; +Cc: gcc-help
On 8 April 2010 05:01, ranjith kumar <ranjithproxy@gmail.com> wrote:
>
> 1) What is the size of an an empty class? On my machine it is showing 1
>
Assuming you mean in C++...
struct Empty {};
Different objects of the same type must have different addresses, so
to allow for arrays, sizeof(Empty) must be > 0.
Empty a[2];
a != a+1 <=> (Empty*)((char*)a + sizeof(Empty))
There is, however, an "empty base class" optimization that the
compiler will try to perform.
struct D : Empty {};
You will probably find that sizeof(D) == sizeof(Empty), rather than
sizeof(D) == 2*sizeof(Empty).
But remember that, for a given type, different object <=> different
address applies to empty base classes too:
struct D2 : D, Empty {};
Because D2 contains 2 sub-objects of type Empty (one a base of D2, one
a base of D), sizeof(D2) >= 2*sizeof(Empty) >= 2.
~ Scott McMurray
^ permalink raw reply [flat|nested] 4+ messages in thread