Thread: Determining the Size of Class Objects tutorial question

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question Determining the Size of Class Objects tutorial question

    In the tutorial at this link, there is the following statement:

    <2>Order of data members The order in which one specifies data members also alters the size of the class.
    Code:
    class C { 
            char c; 
            int int1; 
            int int2; 
            int i; 
            long l; 
            short s; 
    };
    The size of this class is 24 bytes. Even though char c will consume only 1 byte, 4 bytes will be allocated for it, and the remaining 3 bytes will be wasted (holes). This is because the next member is an int, which takes 4 bytes. If we don't go to the next (4th) byte for storing this integer member, the memory access/modify cycle for this integer will be 2 read cycles. So the compiler will do this for us, unless we specify some byte padding/packing.

    If I re-write the above class in different order, keeping all my data members like below:
    Code:
    class C { 
            int int1; 
            int int2; 
            int i; 
            long l; 
            short s; 
            char c; 
    };
    Now the size of this class is 20 bytes.

    In this case, it is storing c, the char, in one of the slots in the hole in the extra four bytes.
    The part in question is the bold part. The author first says the the size of the first code example class is 24 bytes, and then in the second code example, he changed the order of the members to put the char variable last, says the size of this class is 20 bytes, and then follows that up immediately with a statement saying it is storing the char in one of the slots in "the hole in the extra four bytes". Now, I don't know about you, but that doesn't really make any sense to me. If the char, even after putting it at the end of the class instead of the beginning, is still getting 4 bytes of memory allocated for it, of which only 1 byte is actually being used, then why has the class become only 20 bytes now instead of 24 (if the author is correct, I haven't tested it yet)? I also don't understand why the char would have 4 bytes allocated for it instead of one, even if its followed by an int definition in the class. To me, both versions of the class should allocate 21 bytes for an object of that class, not 24.

    Maybe someone would care to explain a little better than it was in the tutorial?

    Thanks in advance.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    90
    Any integral type has to start at a memory offset in line with its size. A short at 2 bytes can for example start at memory offset 0x0002 or 0x8b58 but not 0x0003 or 0x3bc7. Similarly for an int/long at 4 bytes and a long long at 8 bytes (assuming a 32-bit system).

    In the case of the first example, memory is laid out like this:

    Code:
    0     char c
    1-3   unused
    4-7   int int1
    8-11  int int2
    12-15 int i
    16-19 long l
    20-21 short s
    22-23 unused
    In the second example it looks like this:

    Code:
    0-3   int int1
    4-7   int int2
    8-11  int i
    12-15 long l
    16-17 short s
    18    char c
    19    unused
    In both cases, the struct has to fill out a 4 byte word size at the end (on a 32-bit system).

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    The author means that every object will be allocated in N x 4 bytes of a chunk of memory. So if you have 1000 objects, they would allocate 1000 x 4 bytes. Each object will have 3 bytes of "holes", unused space.
    EDIT: better explained above

    I guess that is referred as "byte alignment"
    As mentioned above, if we specify 1 byte alignment, the size of the class above (class C) will be 19 in both cases.
    so that the above examples where assuming 4 byte alignment.

    Of course to optimize maybe the compiler will use int for everything to calculate the offset faster and you can be right.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Whats the best way to control a global class?
    By parad0x13 in forum C++ Programming
    Replies: 3
    Last Post: 11-12-2009, 05:17 PM
  2. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  3. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  4. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  5. An exercise in optimization
    By Prelude in forum Contests Board
    Replies: 10
    Last Post: 04-29-2005, 03:06 PM