public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: me22 <me22.ca@gmail.com>
To: ranjith kumar <ranjithproxy@gmail.com>
Cc: gcc-help@gcc.gnu.org
Subject: Re: sizeof empty class
Date: Fri, 09 Apr 2010 05:03:00 -0000	[thread overview]
Message-ID: <x2rfa28b9251004082203se7dec54ei53c5156c88a91e44@mail.gmail.com> (raw)
In-Reply-To: <t2u31cff80d1004080201gb7337940m34494bdebbf747b@mail.gmail.com>

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

      parent reply	other threads:[~2010-04-09  5:03 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
2010-04-09  3:38 ` leon zadorin
2010-04-09  5:03 ` me22 [this message]

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=x2rfa28b9251004082203se7dec54ei53c5156c88a91e44@mail.gmail.com \
    --to=me22.ca@gmail.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).