public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: "John (Eljay) Love-Jensen" <eljay@adobe.com>
To: ranjith kumar <ranjithproxy@gmail.com>,
	        "gcc-help@gcc.gnu.org" 	<gcc-help@gcc.gnu.org>
Subject: RE: sizeof empty class
Date: Thu, 08 Apr 2010 11:20:00 -0000	[thread overview]
Message-ID: <4B7A6CC9992C4E4FB188D02872C90A6B8A014203@nambxv01a.corp.adobe.com> (raw)
In-Reply-To: <t2u31cff80d1004080201gb7337940m34494bdebbf747b@mail.gmail.com>

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

  reply	other threads:[~2010-04-08 11:20 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-04-08  9:01 ranjith kumar
2010-04-08 11:20 ` John (Eljay) Love-Jensen [this message]
2010-04-09  3:38 ` leon zadorin
2010-04-09  5:03 ` me22

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4B7A6CC9992C4E4FB188D02872C90A6B8A014203@nambxv01a.corp.adobe.com \
    --to=eljay@adobe.com \
    --cc=gcc-help@gcc.gnu.org \
    --cc=ranjithproxy@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).