sizeof class ?

Discussion in 'C++' started by Sameer, Apr 25, 2006.

  1. Sameer

    Sameer Guest

    Hi,
    I have a class.
    class C1
    {
    int i ;
    char c ;
    } ;

    let me say the size of int is 4 and char is 1. Then if I say sizeof(C1)
    it should be 5, why is it showing 8 ?
    Sameer, Apr 25, 2006
    #1
    1. Advertising

  2. Sameer

    Ian Collins Guest

    Sameer wrote:
    > Hi,
    > I have a class.
    > class C1
    > {
    > int i ;
    > char c ;
    > } ;
    >
    > let me say the size of int is 4 and char is 1. Then if I say sizeof(C1)
    > it should be 5, why is it showing 8 ?
    >

    Because your compiler aligns data on 4 byte boundaries, which is typical
    on a 32 bit system.

    --
    Ian Collins.
    Ian Collins, Apr 25, 2006
    #2
    1. Advertising

  3. Sameer

    void Guest

    so concise! Right !
    void, Apr 25, 2006
    #3
  4. Sameer

    Ian Collins Guest

    void wrote:
    > so concise! Right !
    >

    ? please quite some context in your reply.

    --
    Ian Collins.
    Ian Collins, Apr 25, 2006
    #4
  5. Sameer

    Sameer Guest

    So how can I correct this? Is it only possible that the size of a class
    is only multiple of size of int ?
    Sameer, Apr 27, 2006
    #5
  6. Sameer

    Jim Langston Guest

    "Sameer" <> wrote in message
    news:...
    > So how can I correct this? Is it only possible that the size of a class
    > is only multiple of size of int ?


    What do you mean by correcting it? It is correct. What is it about the
    size that you don't like?

    It may be possible to compact the class/structure by a compiler specific
    switch, but this can cause problems. There are some systems that an
    unaligned value can cause the CPU to not read it correctly. It can also
    slow down operation.

    I would suggest you just leave it alone and don't worry about the 3 extra
    bytes.

    If you really must compact it for some reason, look at your compilers
    switches and such. If you're using a microsoft compiler look at #pragma
    pack
    Jim Langston, Apr 27, 2006
    #6
  7. Sameer

    Sameer Guest

    That question (How to make the size fo class exactly as the total size
    of variables defined?) was asked with me in 2 interviews. Thats why i
    asked.
    Sameer, May 17, 2006
    #7
  8. Sameer

    Guest

    first, what is the size of an empty class? what the empty class
    contains?
    , May 17, 2006
    #8
  9. Sameer

    Rolf Magnus Guest

    wrote:

    > first, what is the size of an empty class?


    Anything bigger than zero.

    > what the empty class contains?


    Padding bytes that ensure that the size is not zero.
    Rolf Magnus, May 17, 2006
    #9
  10. Sameer

    Marcus Kwok Guest

    Rolf Magnus <> wrote:
    > wrote:
    >
    >> first, what is the size of an empty class?

    >
    > Anything bigger than zero.


    ....as long as it is not an empty base class for a derived class. In
    that case, it can be zero. However, on its own, I agree it must be
    nonzero, so that distinct objects can be distinguished.


    #include <iostream>

    class Base { };

    class Derived : public Base {
    int x;
    };

    class OneInt {
    int x;
    };

    int main()
    {
    std::cout << "sizeof int: " << sizeof(int) << '\n';
    std::cout << "sizeof Base: " << sizeof Base << '\n';
    std::cout << "sizeof Derived: " << sizeof Derived << '\n';
    std::cout << "sizeof OneInt: " << sizeof OneInt << '\n';
    }


    /*
    Output on my machine:
    sizeof int: 4
    sizeof Base: 1
    sizeof Derived: 4
    sizeof OneInt: 4
    */

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
    Marcus Kwok, May 17, 2006
    #10
    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,303
    Ron Natalie
    Oct 14, 2004
  2. Trevor

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

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    614
    CBFalconer
    Apr 10, 2004
  3. Vinu
    Replies:
    13
    Views:
    1,383
    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:
    144
    Robert Klemme
    Dec 31, 2004
Loading...

Share This Page