Thread: size of a class

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    29

    size of a class

    Hi.

    I have this class.
    Code:
    class ab{
         int a;
         char b;
        };
    
    class cd{
       char c;
       int d;
    };
    what will be the size of these two classes? Will the value be different if i run it on windows and linux OS?

    Thanks.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    They will (probably) both be 8 bytes, but there's no guarantee. The compiler can padd or add extra bytes, and this varies from each compiler, so there's no 100% guaranteed answer. It does not depend on OS, however. Only the compilers.
    Note that these classes will probably be the same size, however.

    You can use sizeof to determine the size of the classes at runtime in your compiler.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You can use sizeof to determine the size of the classes at runtime in your compiler.
    At compile time, actually.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That's a difficult question to answer, since "on Windows" and "on Linux" doesn't tell the whole story.

    The key here is that char and int have different sizes, and different alignment criteria.

    The "normal" alignment is that components in a struct or class is aligned to the size of the component, and the class itself has a padding to make the NEXT class provide the same alignment. So, by this, we can figure that the size of both of your classes, in normal case is "2 * sizeof(int)" - because the int needs to be aligned to it's own size, and the whole class needs to be a multiple of that size.

    However, the only element that we can say for sure what size it is, without knowing the compiler vendor and possibly settings, is the char type (sizeof(char) is always one in ALL compilers for ALL architectures). All other types (short, int, float, long, double, long long, extended double or whatever they may be) are dependant on the compiler implementation - we can have a good guess that int is 4 bytes, but it's by no means SURE that this is the case.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I was more referring to that you can see it at compile time. But you're right, of course. The size is evaluated at compile time.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Why not check the size for yourself and find out?

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    its 8 in both cases.......

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by sunny_master_07 View Post
    its 8 in both cases.......
    It's 8 only on Sundays and Thursdays. On Wednesdays it's 5, and on the other days of the week, it's 11.

    Unless it's a leap year, in which case it's always 13.

    Anyway, the point is, use sizeof.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by sunny_master_07 View Post
    its 8 in both cases.......
    As brewbuck is trying to say: In the cases you have so far seen, it's 8 in both cases. But it's not absolutely guaranteed. For example, if you use visual C++ and give it the option /Zp1, it would be 5 for both of them.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by brewbuck View Post
    It's 8 only on Sundays and Thursdays. On Wednesdays it's 5, and on the other days of the week, it's 11.

    Unless it's a leap year, in which case it's always 13.
    Well, I do hope the compiler has a more stable ABI than that
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Lol, you gave me a good laugh there, CornedBee. Thanks
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  4. An exercise in optimization
    By Prelude in forum Contests Board
    Replies: 10
    Last Post: 04-29-2005, 03:06 PM
  5. Replies: 11
    Last Post: 03-25-2003, 05:13 PM