Thread: structure packing

  1. #1
    Registered User
    Join Date
    Jul 2007
    Location
    Hyderabad
    Posts
    35

    structure packing

    hi,

    structure padding is the way to put padding bits/bytes depening on byte boundary.

    but where structure packing comes, as i know that it supresses the padding bits.if it so, is the processor able to read that paricular memory efficiently.
    supressing means what actually will happen in memory.
    Code:
    struct 
    {
            char a;
            char b;
            char c;
            int i;
    };
    --->here     1 byte+1 byte + 1 byte + padding byte + 4 bytes = 8 bytes
    
    sturct 
    {
          char a;
          char b;
          char c;
          int i;
    }__attribute__((packed));
    --->here 1 byte+1 byte+1 byte+4 bytes=7 bytes.

    supressing of padding bytes means what?.but then no byte boundary here.

    please tell me what is it?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Some CPUs, particular from Intel, don't have to have everything lined up along boundaries, although performance significantly suffers.

    This is a rather system specific and even compiler specific topic.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right, the padding is there for a reason:
    reading an int from an address that is aligned with the size of the int makes it much faster.
    On some CPU's it's even invalid to read unaligned data types - so the member "i" in your packed struct could not be read directly on a 68000 machine, for example, an attempt to do so would trap into a "unaligned access" handler - which of course COULD fix up the problem, but now reading a single integer, which is a relatively simple operation, is taking hundreds of clock-cycles. Most machines with this type of architecture will CRASH when an unaligned access occurs, rather than fix it up [because it's probably not intentional].

    There are good reasons to pack data structures for compatibility with old architectures, but it's bad for performance and portability.

    --
    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.

  4. #4
    Registered User
    Join Date
    Jul 2007
    Location
    Hyderabad
    Posts
    35
    thank you for reply. from this i got something on structure padding and packing

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 05:46 PM
  2. Replies: 5
    Last Post: 02-14-2006, 09:04 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 05:48 PM