Empty class takes a byte?

Discussion in 'C++' started by avranju@gmail.com, Nov 18, 2005.

  1. Guest

    Hi,

    We have a few exception marker classes in a project that are used only
    to throw exceptions. I mean stuff like this (this may not be such a
    hot idea for doing exception handling - a simple enum might suffice
    since we aren't sending any data with the exception - but that's sort
    of off-topic I suppose):

    class Whoops{};
    class GeeDontKnowHowToDoThat{};

    Just out of curiosity I wanted to see if such classes actually consumed
    any memory. The following program,

    #include <iostream>

    using namespace std;

    class Foo{};

    int main()
    {
    cout<<"sizeof( Foo ) = "<<sizeof( Foo )<<endl;
    Foo *p = new Foo;
    cout<<(int)*( (char *)p )<<endl;
    delete p;
    return 0;
    }

    produces the following output on MSVC++ 7.1:

    sizeof( Foo ) = 1
    120

    So, looks like an empty class like this still consumes 1 byte of memory
    and some random data is put there. Is that really the case?

    Thanks!

    --
    Ranju. V
    http://www.geocities.com/cool_ranju/
    --
     
    , Nov 18, 2005
    #1
    1. Advertising

  2. wrote:
    > So, looks like an empty class like this still consumes 1 byte of memory
    > and some random data is put there. Is that really the case?
    >


    An empty class cannot have a zero size - what will be the address of
    an instance of such a class? Observe that two different variables will
    end up having the same memory address under such circumstances. Hence
    size of empty class is usually 1.
     
    Neelesh Bodas, Nov 18, 2005
    #2
    1. Advertising

  3. Guest

    写é“:

    > Hi,
    >
    > We have a few exception marker classes in a project that are used only
    > to throw exceptions. I mean stuff like this (this may not be such a
    > hot idea for doing exception handling - a simple enum might suffice
    > since we aren't sending any data with the exception - but that's sort
    > of off-topic I suppose):
    >
    > class Whoops{};
    > class GeeDontKnowHowToDoThat{};
    >
    > Just out of curiosity I wanted to see if such classes actually consumed
    > any memory. The following program,
    >
    > #include <iostream>
    >
    > using namespace std;
    >
    > class Foo{};
    >
    > int main()
    > {
    > cout<<"sizeof( Foo ) = "<<sizeof( Foo )<<endl;
    > Foo *p = new Foo;
    > cout<<(int)*( (char *)p )<<endl;
    > delete p;
    > return 0;
    > }
    >
    > produces the following output on MSVC++ 7.1:
    >
    > sizeof( Foo ) = 1
    > 120
    >
    > So, looks like an empty class like this still consumes 1 byte of memory
    > and some random data is put there. Is that really the case?


    if you define two data like this :
    Foo *p = new Foo;
    Foo *p1 = new Foo;
    you want the p and p1not to be the same,so the c++'s father put 1 byte
    of memory in each empty class.
    and your code in this line: cout<<(int)*( (char *)p )<<endl;
    really needs to write like this :cout<<*( (char *)p )<<endl; to prove
    you case.
     
    , Nov 18, 2005
    #3
  4. Ferdi Smit Guest

    wrote:
    > Hi,
    >


    > So, looks like an empty class like this still consumes 1 byte of memory
    > and some random data is put there. Is that really the case?
    >


    Yes:

    "A class with an empty sequence of members and base class objects is an
    empty class. Complete objects and member subobjects of an empty class
    type shall have nonzero size. 1)

    1) That is, a base class subobject of an empty class type may have zero
    size."

    So there is one allowed optimization when the empty class is a base
    class. This is known as the empty-base class optimization. Note that a
    compiler is not required to implement this.

    A nice page describing this feature and some of its uses can be found
    here: http://www.cantrip.org/emptyopt.html

    --
    Regards,

    Ferdi Smit (M.Sc.)
    Email:
    Room: C0.07 Phone: 4229
    INS3 Visualization and 3D Interfaces
    CWI Amsterdam, The Netherlands
     
    Ferdi Smit, Nov 18, 2005
    #4
  5. Ranju. V Guest

    Ranju. V, Nov 18, 2005
    #5
    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. John Carson
    Replies:
    10
    Views:
    21,029
    cplusprog
    Apr 18, 2010
  2. Alexander Terekhov

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

    Alexander Terekhov, Jul 7, 2003, in forum: C++
    Replies:
    0
    Views:
    422
    Alexander Terekhov
    Jul 7, 2003
  3. raghav
    Replies:
    0
    Views:
    676
    raghav
    Jul 7, 2003
  4. Sweety
    Replies:
    4
    Views:
    7,586
    rossum
    Aug 21, 2004
  5. ishekara
    Replies:
    8
    Views:
    10,806
    Mike Wahler
    Jan 5, 2005
Loading...

Share This Page