clarification req: 4 new article in cprogramming.com

This is a discussion on clarification req: 4 new article in cprogramming.com within the C++ Programming forums, part of the General Programming Boards category; Code: class C { char c; int int1; int int2; int i; long l; short s; }; The size of ...

  1. #1
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203

    Unhappy clarification req: 4 new article in cprogramming.com

    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 next 4th byte for storing this integer member, the memory access/modify cycle for this integer will be 2 read cycle. So the compiler will do this for us, unless we specify some byte padding/packing.
    This article was in cprogramming.com. When using sizeof() I have never noticed such a feature.
    If this wastes memmory, Why is this done?
    and if this size thingi is compiler specific, how can one ensure poratability of the prgram?
    More over I have never seen this stuff in any of the few books I've read.
    Can some one please explain this???

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    when arranging structors the compiler aligns all numeric data types (short, long, _int64, floats and doubles) and user-defined data types (e.g. other structures) on even byte boundry for faster access time. And yes, the default byte alignment is compiler specific, and yes, that can be a big problem when transferring data from one computer or compiler to another. Most database programs use single byte alignment for compatible purposes and to keep file size as small as possible-- they don't want or need all that wasted file space.(see below example)


    Most compilers have switches or #pragma to override the default. For example, VC++ 6.0 is like this
    Code:
    #pragma pack(1) // align all items on single byte boundry
    class C { 
            char c; 
            int int1; 
            int int2; 
            int i; 
            long l; 
            short s; 
    }; 
    #pragma pack() // use default byte alignment
    In the above example, there will be no holes in the class (or structure)
    Last edited by Ancient Dragon; 09-03-2005 at 11:29 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Article in GDM about DarkBasic PRO
    By jasonkidd05 in forum Game Programming
    Replies: 5
    Last Post: 02-03-2008, 04:47 AM
  2. Computer Classes (Article)
    By LiNeAr in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 08-18-2005, 03:04 PM
  3. Article in GDM about DarkBasic PRO
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 09-03-2003, 07:27 PM
  4. Recent Slashdot article.
    By spoon_ in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 05-09-2003, 04:41 AM
  5. Writing an article
    By jverkoey in forum Game Programming
    Replies: 24
    Last Post: 03-02-2003, 10:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21