sizeof empty class

Discussion in 'C++' started by deepakvsoni@gmail.com, Sep 5, 2007.

  1. Guest

    what is the size of an class with not data members?
    ex:
    class A {
    };

    also
    sizeof ?
    class A{
    int f(){
    cout<<"Hello";
    }
    };
     
    , Sep 5, 2007
    #1
    1. Advertising

  2. Jim Langston Guest

    <> wrote in message
    news:...
    > what is the size of an class with not data members?
    > ex:
    > class A {
    > };
    >
    > also
    > sizeof ?
    > class A{
    > int f(){
    > cout<<"Hello";
    > }
    > };


    Usually 1. Why don't you try it?
     
    Jim Langston, Sep 5, 2007
    #2
    1. Advertising

  3. Guest

    On Sep 5, 8:51 am, "Jim Langston" <> wrote:
    > <> wrote in message
    >
    > news:...
    >
    > > what is the size of an class with not data members?
    > > ex:
    > > class A {
    > > };

    >
    > > also
    > > sizeof ?
    > > class A{
    > > int f(){
    > > cout<<"Hello";
    > > }
    > > };

    >
    > Usually 1. Why don't you try it?


    i've tried it... but why is it 1? Is it just for memory allocation so
    that no two objects are allocated the same space?
     
    , Sep 5, 2007
    #3
  4. Jim Langston Guest

    <> wrote in message
    news:...
    > On Sep 5, 8:51 am, "Jim Langston" <> wrote:
    >> <> wrote in message
    >>
    >> news:...
    >>
    >> > what is the size of an class with not data members?
    >> > ex:
    >> > class A {
    >> > };

    >>
    >> > also
    >> > sizeof ?
    >> > class A{
    >> > int f(){
    >> > cout<<"Hello";
    >> > }
    >> > };

    >>
    >> Usually 1. Why don't you try it?

    >
    > i've tried it... but why is it 1? Is it just for memory allocation so
    > that no two objects are allocated the same space?


    Yes, among other considerations.
     
    Jim Langston, Sep 5, 2007
    #4
  5. Guest

    On Sep 5, 9:43 am, "Jim Langston" <> wrote:
    > <> wrote in message
    >
    > news:...
    >
    >
    >
    >
    >
    > > On Sep 5, 8:51 am, "Jim Langston" <> wrote:
    > >> <> wrote in message

    >
    > >>news:...

    >
    > >> > what is the size of an class with not data members?
    > >> > ex:
    > >> > class A {
    > >> > };

    >
    > >> > also
    > >> > sizeof ?
    > >> > class A{
    > >> > int f(){
    > >> > cout<<"Hello";
    > >> > }
    > >> > };

    >
    > >> Usually 1. Why don't you try it?

    >
    > > i've tried it... but why is it 1? Is it just for memory allocation so
    > > that no two objects are allocated the same space?

    >
    > Yes, among other considerations.- Hide quoted text -
    >
    > - Show quoted text -


    so you mean to say the actual size of object is 0 but it is allocated
    atleast 1 byte..
     
    , Sep 5, 2007
    #5
  6. James Kanze Guest

    On Sep 5, 5:54 am, "" <>
    wrote:
    > On Sep 5, 8:51 am, "Jim Langston" <> wrote:
    > > <> wrote in message


    > >news:...


    > > > what is the size of an class with not data members?
    > > > ex:
    > > > class A {
    > > > };


    > > > also
    > > > sizeof ?
    > > > class A{
    > > > int f(){
    > > > cout<<"Hello";
    > > > }
    > > > };


    > > Usually 1. Why don't you try it?


    It's implementation defined. It's usually 1 on byte addressed
    machines, but probably the same as sizeof(int) on a word
    addressed machine.

    > i've tried it... but why is it 1? Is it just for memory allocation so
    > that no two objects are allocated the same space?


    It's principally so that no two objects (of the same type) have
    the same address. If you write something like:
    A array[2] ;
    you are guaranteed that &array[0] != &array[1].

    Note that there are certain, very special cases where the
    compiler is allowed to allocate less memory than the size of the
    object. For example, given:

    class A {} ;
    class B : public A { int i ;} ;

    With many compilers, sizeof( B ) == sizeof( int ), even though
    sizeof( A ) + sizeof( int ) is larger. (This is called the
    "empty base class" optimization.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Sep 5, 2007
    #6
  7. Jim Langston Guest

    <> wrote in message
    news:...
    > On Sep 5, 9:43 am, "Jim Langston" <> wrote:
    >> <> wrote in message
    >>
    >> news:...
    >>
    >> > On Sep 5, 8:51 am, "Jim Langston" <> wrote:
    >> >> <> wrote in message

    >>
    >> >>news:...

    >>
    >> >> > what is the size of an class with not data members?
    >> >> > ex:
    >> >> > class A {
    >> >> > };

    >>
    >> >> > also
    >> >> > sizeof ?
    >> >> > class A{
    >> >> > int f(){
    >> >> > cout<<"Hello";
    >> >> > }
    >> >> > };

    >>
    >> >> Usually 1. Why don't you try it?

    >>
    >> > i've tried it... but why is it 1? Is it just for memory allocation so
    >> > that no two objects are allocated the same space?

    >>
    >> Yes, among other considerations
    >>

    > so you mean to say the actual size of object is 0 but it is allocated
    > atleast 1 byte..


    No, the size of the object is 1 byte. The compiler adds a padding byte.
    Read James message, an array of 0 sized objects wouldn't work.
     
    Jim Langston, Sep 5, 2007
    #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,421
    Ron Natalie
    Oct 14, 2004
  2. JK
    Replies:
    2
    Views:
    4,066
  3. Trevor

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

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    663
    CBFalconer
    Apr 10, 2004
  4. GRenard
    Replies:
    2
    Views:
    446
    Salt_Peter
    Nov 3, 2006
  5. Nikolai Weibull

    sizeof(Class)/sizeof(Object)

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

Share This Page