sizeof class

Discussion in 'C++' started by asit, Oct 10, 2011.

  1. asit

    asit Guest

    Please go through the following code...


    #include <iostream>

    using namespace std;

    class Empty {};

    class Derived1 : public Empty {};

    class Derived2 : virtual public Empty {};

    class Derived3 : public Empty
    {
    char c;
    };

    class Derived4 : virtual public Empty
    {
    char c;
    };

    class Dummy
    {
    char c;
    };

    int main()
    {
    cout<<"Sizeof(Empty) : "<<sizeof(Empty)<<endl;
    cout<<"Sizeof(Derived1) : "<<sizeof(Derived1)<<endl;
    cout<<"Sizeof(Derived2) : "<<sizeof(Derived2)<<endl;
    cout<<"Sizeof(Derived3) : "<<sizeof(Derived3)<<endl;
    cout<<"Sizeof(Derived4) : "<<sizeof(Derived4)<<endl;
    cout<<"Sizeof(Dummy) : "<<sizeof(Dummy)<<endl;

    return 0;
    }


    Why sizeof Derived4 is 8 bytes ?
    asit, Oct 10, 2011
    #1
    1. Advertising

  2. Am 10.10.2011 20:22, schrieb asit:

    > Why sizeof Derived4 is 8 bytes ?


    In this case the compiler inserted a four bytes pointer (you're on
    a 32-bit-environment) to the virtual base-class. That's usual with
    virtual base-classes - but implementation defined.
    Gerald Breuer, Oct 10, 2011
    #2
    1. Advertising

  3. asit

    asit Guest

    Hence the size should be 5 bytes(4 for v-pointer + 1 for char). Then why its is of 8 bytes ?
    asit, Oct 10, 2011
    #3
  4. asit

    Ian Collins Guest

    On 10/11/11 08:13 AM, asit wrote:

    What are you replying to? Please keep context.

    > Hence the size should be 5 bytes(4 for v-pointer + 1 for char). Then why its is of 8 bytes ?


    The size of a class with a virtual base is implementation defined.

    --
    Ian Collins
    Ian Collins, Oct 10, 2011
    #4
  5. On 10/10/2011 3:23 PM, Ian Collins wrote:
    > On 10/11/11 08:13 AM, asit wrote:
    >
    > What are you replying to? Please keep context.
    >
    >> Hence the size should be 5 bytes(4 for v-pointer + 1 for char). Then
    >> why its is of 8 bytes ?

    >
    > The size of a class with a virtual base is implementation defined.


    I am not even sure that implementation-definedness is limited to classes
    with virtual bases. To OP: have you tried printing out the value sizeof
    yields for a class with a single 'char' data member?

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Oct 10, 2011
    #5
  6. Am 10.10.2011 21:13, schrieb asit:

    > Hence the size should be 5 bytes(4 for v-pointer + 1 for char). Then why its is of 8 bytes ?


    That's also implementation-defined. But usually your compiler
    adds some padding so that each elment in an array of objects
    is properly aligned. I'm pretty sure the implementation of
    your compiler has a #pragma pack(xyz) directive.
    Gerald Breuer, Oct 10, 2011
    #6
  7. On 10.10.2011 21:13, asit wrote:
    > Hence the size should be 5 bytes(4 for v-pointer + 1 for char). Then why its is of 8 bytes ?


    Can you say padding? The alignment of the structure resulting from the
    class definition would be four bytes, but it has a size of five bytes.
    To enforce alignment in aggregate types, the size is adjusted to be
    divisible by four.

    HTH,
    Markus
    Markus Wichmann, Oct 10, 2011
    #7
    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. Derek
    Replies:
    7
    Views:
    24,304
    Ron Natalie
    Oct 14, 2004
  2. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    615
    CBFalconer
    Apr 10, 2004
  3. Vinu
    Replies:
    13
    Views:
    1,386
    Lawrence Kirby
    May 12, 2005
  4. GRenard
    Replies:
    2
    Views:
    407
    Salt_Peter
    Nov 3, 2006
  5. Nikolai Weibull

    sizeof(Class)/sizeof(Object)

    Nikolai Weibull, Dec 31, 2004, in forum: Ruby
    Replies:
    2
    Views:
    145
    Robert Klemme
    Dec 31, 2004
Loading...

Share This Page