CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Aug 2000
    Posts
    1,471

    A question regarding the size of a class.

    Say we have a class defined as,

    class c
    {
    char c1;
    char c2;
    int i1;
    int i2;
    char *ptr;
    static int mem;
    };

    What is the size of the class c on a 32 bit processor? Here is what I think.

    char c1: 1 byte
    char c2: 1 byte
    int i1: 4 bytes
    int i2: 4 bytes
    char* ptr: 4 bytes
    static int mem: 4 bytes

    Totally, 18 bytes. Am I right? Thanks for your inputs.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: A question regarding the size of a class.

    I do not think the static member will be counted. There may also be padding after the two char members (adding another 2 bytes), making it 4 + 4 + 4 + 4 = 16 bytes.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Aug 2000
    Posts
    1,471

    Re: A question regarding the size of a class.

    Thanks for your response! Why will the static member NOT be counted? As for padding, why isn't there padding for each of c1 and c2? Thanks for your inputs.
    Quote Originally Posted by laserlight
    I do not think the static member will be counted. There may also be padding after the two char members (adding another 2 bytes), making it 4 + 4 + 4 + 4 = 16 bytes.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: A question regarding the size of a class.

    Why will the static member NOT be counted?
    According to the C++ Standard, "a static data member is not part of the subobjects of a class".

    As for padding, why isn't there padding for each of c1 and c2?
    Since they are adjacent, together they can fit in the space of an int.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: A question regarding the size of a class.

    Quote Originally Posted by laserlight
    Since they are adjacent, together they can fit in the space of an int.
    Padding is very much implementation dependant. The compiler could (but probably would not) pad between the char fields. The compiler could (and quite probably would) pad after the characters but before the int in order to have a 32 bit integer properly aligned.

    As far as the static, think of it this way. You create 1000 instances of the object, there is still only one copy of the static data (that is the very definition). Therefore it is not part of the size of an instance.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  6. #6
    Join Date
    Aug 2000
    Posts
    1,471

    Re: A question regarding the size of a class.

    Thanks for you guys' help! Now I understand the size of the class could be 16 or 20 bytes depending on the compiler.

  7. #7
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: A question regarding the size of a class.

    And don't be fooled that the size of the class below is 0
    Code:
    class A
    {
    }

  8. #8
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: A question regarding the size of a class.

    ... And that you can change the way that the compiler packs the class's structure. For example, in Microsoft's compiler:
    Code:
    struct st1 
    { 
     char c; 
     int i; 
    }; 
    
    #pragma pack ( push )
    #pragma pack ( 1 )
    
    struct st2  /* identical to st1 */
    { 
     char c; 
     int i; 
    };
    
    #pragma pack( pop )
    
    void main() 
    { 
     cout << "Size of st1 :" << sizeof(st1) << endl; 
     cout << "Size of st2 :" << sizeof(st2) << endl; 
    } 
    
    Output (untested):
    
    Size of st1 : 8 
    Size of st2 : 5
    Mike

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured