sizeof(derived) >= sizeof(base)?

Discussion in 'C++' started by cppquester, Oct 7, 2011.

  1. cppquester

    cppquester Guest

    Although I am pretty sure this is true:
    Is it guaranteed, that the size (from sizeof operator) of a derived
    class is always at least as large as the base class?
    Thanks,
    Marc
    cppquester, Oct 7, 2011
    #1
    1. Advertising

  2. cppquester <> wrote:
    > Although I am pretty sure this is true:
    > Is it guaranteed, that the size (from sizeof operator) of a derived
    > class is always at least as large as the base class?


    I don't know what the standard says, so this is complete speculation.
    However, I would imagine that it makes no such guarantee.

    A plausible (but in practice completely hypothetical) situation where
    the derived class might actually be smaller than the base class is when
    so-called empty base class optimization is in play. If the compiler
    decides that the size of an empty class is, let's say, 4 bytes, while
    the size of a class containing one char is 1 byte, you could stumble
    across this very situation, if the latter is derived from the former.

    I'm pretty certain this will never happen in practice, though.
    Juha Nieminen, Oct 7, 2011
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. qazmlp
    Replies:
    1
    Views:
    564
    qazmlp
    Apr 10, 2005
  2. Alex Vinokur
    Replies:
    7
    Views:
    496
    Clark S. Cox III
    Aug 14, 2006
  3. Replies:
    4
    Views:
    404
    Alf P. Steinbach
    May 23, 2007
  4. Replies:
    1
    Views:
    393
    myork
    May 23, 2007
  5. Replies:
    1
    Views:
    385
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page