Re: Why empty class size is 1 Byte ???

Discussion in 'C++' started by John Carson, Jul 7, 2003.

  1. John Carson

    John Carson Guest

    "vijay" <> wrote in message
    news:beb0ok$mvl$
    > > vija > I wanted to know why an empty class size is one byte
    > >
    > > carson >Perhaps so each instance of the class has a distinct
    > > address.

    > vijay > Thanks, But this again raises an other doubt >But when u
    > create a integer the size of class then would then be 4 and not 5
    > bytes,


    You mean a class the size of an integer? On my computer:

    class X
    {
    };

    class Y
    {
    int a;
    };

    int main()
    {
    std::cout << sizeof(X) << '\n';
    std::cout << sizeof(Y) << '\n';
    }

    produces an output of 1 and 4.


    --
    John Carson
    1. To reply to email address, remove donald
    2. Don't reply to email address (post here instead)
     
    John Carson, Jul 7, 2003
    #1
    1. Advertising

  2. John Carson

    vijay Guest

    vija > I wanted to know why an empty class size is one byte

    > carson >Perhaps so each instance of the class has a distinct address.


    > > vijay > Thanks, But this again raises an other doubt >But when u


    > > create a integer the size of class then would then be 4 and not 5 bytes,


    >>>> You mean a class the size of an integer? On my computer:

    >
    > class X
    > {
    > };
    >
    > class Y
    > {
    > int a;
    > };
    >
    > int main()
    > {
    > std::cout << sizeof(X) << '\n';
    > std::cout << sizeof(Y) << '\n';
    > }
    >
    > produces an output of 1 and 4.
    >
    >
    > --
    > John Carson


    Vijay start :post 3
    Hello
    Yes , exactly, I want to know why the size of y is not 5 bytes, since int -
    4 bytes is an addition over 1 byte ?
    Vijay end :post 3
     
    vijay, Jul 7, 2003
    #2
    1. Advertising

  3. John Carson

    John Carson Guest

    "vijay" <> wrote in message
    news:beb1m8$qma$
    > vija > I wanted to know why an empty class size is one byte
    >
    > > carson >Perhaps so each instance of the class has a distinct
    > > address.

    >
    > > > vijay > Thanks, But this again raises an other doubt >But when u

    >
    > > > create a integer the size of class then would then be 4 and not 5
    > > > bytes,

    >
    > > > > > You mean a class the size of an integer? On my computer:

    > >
    > > class X
    > > {
    > > };
    > >
    > > class Y
    > > {
    > > int a;
    > > };
    > >
    > > int main()
    > > {
    > > std::cout << sizeof(X) << '\n';
    > > std::cout << sizeof(Y) << '\n';
    > > }
    > >
    > > produces an output of 1 and 4.
    > >
    > >
    > > --
    > > John Carson

    >
    > Vijay start :post 3
    > Hello
    > Yes , exactly, I want to know why the size of y is not 5 bytes, since
    > int - 4 bytes is an addition over 1 byte ?
    > Vijay end :post 3



    Because compilers are not bound by any rule that says that class size must
    increase by the size of what is added. If you add an extra byte to an empty
    class so each object has a distinct address, then you clearly no longer need
    the extra byte when the class is non-empty. So the compiler discards it (or,
    more accurately, doesn't add it in the first place).

    --
    John Carson
    1. To reply to email address, remove donald
    2. Don't reply to email address (post here instead)
     
    John Carson, Jul 7, 2003
    #3
  4. vijay wrote:

    > Vijay start :post 3
    > Hello
    > Yes , exactly, I want to know why the size of y is not 5 bytes, since int -
    > 4 bytes is an addition over 1 byte ?


    Because it's an "optimization". Inheriting zero length classes does not
    increse the size of the class it is inheriting. However, to instantiate
    a zero length class will mean that the minimum size of 1 byte needs to
    be allocates so that an array may have distinct addresses.

    Early compilers had an overhead and would have had a size implication on
    inherited classes. The latest compilers do this optimization so that
    you can inherit as many zero length classes as you want and have no
    impact on memory utilization in the final class.

    How would you like it to work ?

    G
     
    Gianni Mariani, Jul 7, 2003
    #4
  5. "Gianni Mariani" <> wrote in message
    news:beb2ui$...
    > vijay wrote:
    >
    > > Vijay start :post 3
    > > Hello
    > > Yes , exactly, I want to know why the size of y is not 5 bytes, since

    int -
    > > 4 bytes is an addition over 1 byte ?

    >
    > Because it's an "optimization". Inheriting zero length classes does not
    > increse the size of the class it is inheriting. However, to instantiate
    > a zero length class will mean that the minimum size of 1 byte needs to
    > be allocates so that an array may have distinct addresses.
    >
    > Early compilers had an overhead and would have had a size implication on
    > inherited classes. The latest compilers do this optimization so that
    > you can inherit as many zero length classes as you want and have no
    > impact on memory utilization in the final class.
    >
    > How would you like it to work ?
    >
    > G
    >


    There isn't any inheritance in John Carson's example, which makes Vijay's
    objections even harder to understand.

    john
     
    John Harrison, Jul 7, 2003
    #5
  6. Gianni Mariani wrote:
    [...]
    > Because it's an "optimization". Inheriting zero length classes does not
    > increse the size of the class it is inheriting. However, to instantiate
    > a zero length class ....


    There's no such thing as "zero length class" in C++. Sizeof is always
    positive in C++ (that's not the case in C99, BTW).

    regards,
    alexander.
     
    Alexander Terekhov, Jul 7, 2003
    #6
  7. "vijay" <> wrote in message
    news:beb1m8$qma$...

    > vija > I wanted to know why an empty class size is one byte


    > > carson >Perhaps so each instance of the class has a distinct address.


    > > > vijay > Thanks, But this again raises an other doubt >But when u
    > > > create a integer the size of class then would then be 4 and not 5

    bytes,

    > >>>> You mean a class the size of an integer? On my computer:

    > >
    > > class X
    > > {
    > > };
    > >
    > > class Y
    > > {
    > > int a;
    > > };
    > >
    > > int main()
    > > {
    > > std::cout << sizeof(X) << '\n';
    > > std::cout << sizeof(Y) << '\n';
    > > }
    > >
    > > produces an output of 1 and 4.
    > >
    > >
    > > --
    > > John Carson

    >
    > Vijay start :post 3
    > Hello
    > Yes , exactly, I want to know why the size of y is not 5 bytes, since

    nt -
    > 4 bytes is an addition over 1 byte ?
    > Vijay end :post 3



    Try to think of it this way: class Y above is as large as the 'int' it
    contains, which is 4 bytes. Class X is also as large as the data member it
    contains, and since it contains no data members, that is 0 bytes. But
    because a class/struct cannot be 0 bytes, because each instance of the
    class/struct must have its own unique address (as JohnCarsonalready told
    you), the compiler just adds a byte for padding.
    But that is not the only case where the compiler adds additional bytes
    to your class. For the following class, most compiler add a padding byte:

    class Z
    {
    int a,
    char b[3];
    };

    sizeo (Z) is 8 on a lot compilers, even though you might expect it to be
    7.

    hth
    --
    jb

    (replace y with x if you want to reply by e-mail)
     
    Jakob Bieling, Jul 7, 2003
    #7
  8. Alexander Terekhov wrote:
    > Gianni Mariani wrote:
    > [...]
    >
    >>Because it's an "optimization". Inheriting zero length classes does not
    >>increse the size of the class it is inheriting. However, to instantiate
    >>a zero length class ....

    >
    >
    > There's no such thing as "zero length class" in C++. Sizeof is always
    > positive in C++ (that's not the case in C99, BTW).
    >
    > regards,
    > alexander.




    #include <iostream>

    using namespace std;

    struct A
    {
    };

    struct B : A
    {
    int a;
    };

    int main()
    {

    cout << "sizeof(A) : " << sizeof(A) << endl;
    cout << "sizeof(B) : " << sizeof(B) << endl;
    }

    $ ./class
    sizeof(A) : 1
    sizeof(B) : 4


    When A is inherited into B, A is contributing nothing to the size of B.

    Hence, A is a "zero length" class in this case (when inherited in B).

    Did I imagine somthing ?
     
    Gianni Mariani, Jul 7, 2003
    #8
  9. Gianni Mariani wrote:
    [...]
    > When A is inherited into B, A is contributing nothing to the size of B.
    >
    > Hence, A is a "zero length" class in this case (when inherited in B).
    >
    > Did I imagine somthing ?


    Sort of. A is an empty class. An empty base class sub-objects may not
    occupy storage -- that's an optimization known as EBO.

    regards,
    alexander.
     
    Alexander Terekhov, Jul 7, 2003
    #9
  10. John Carson

    rajesh64727

    Joined:
    Sep 23, 2008
    Messages:
    1
    Class size

    It is just because an empty class is initialized by NULL. and the NULL is nothing but a char type , i.e size of 1.

    When you declare some datatype in it, the class is not longer empty (NULL) type, so with an integer type size of class is 4 and not 5.



    Rajesh Kumar
    Gameshastra Soln
     
    rajesh64727, Sep 23, 2008
    #10
  11. John Carson

    cplusprog

    Joined:
    Apr 18, 2010
    Messages:
    1
    Size of an empty class.

    Its one byte, also don't forget to read about Empty base optimization..

    You can listen to voice notes on empty c++ class at listenvoice.com
     
    cplusprog, Apr 18, 2010
    #11
    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. Alexander Terekhov

    Re: Why empty class size is 1 Byte ???

    Alexander Terekhov, Jul 7, 2003, in forum: C++
    Replies:
    0
    Views:
    414
    Alexander Terekhov
    Jul 7, 2003
  2. raghav
    Replies:
    0
    Views:
    666
    raghav
    Jul 7, 2003
  3. Sweety
    Replies:
    4
    Views:
    7,516
    rossum
    Aug 21, 2004
  4. ishekara
    Replies:
    8
    Views:
    10,741
    Mike Wahler
    Jan 5, 2005
  5. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,982
    Smokey Grindel
    Dec 2, 2006
Loading...

Share This Page