Byte Alignment

Discussion in 'C Programming' started by Shashi, Nov 10, 2003.

  1. Shashi

    Shashi Guest

    Can somebody explain how the byte alignment for structures work,
    taking the following example and considering:
    byte of 1 Byte
    word of 2 Bytes
    dword of 4 Bytes

    typedef struct
    {
    byte a;
    word b;
    dword c;
    byte d;
    }foo;

    foo foo1;
    x=sizeof(foo1);

    What will be the value of x in different cases,
    For One Byte Alignment
    For Two Byte Alignment
    For Four Byte Alignment
    It will be nice if I get a good explaination too...Thanx
     
    Shashi, Nov 10, 2003
    #1
    1. Advertising

  2. Shashi <> scribbled the following:
    > Can somebody explain how the byte alignment for structures work,
    > taking the following example and considering:
    > byte of 1 Byte
    > word of 2 Bytes
    > dword of 4 Bytes


    > typedef struct
    > {
    > byte a;
    > word b;
    > dword c;
    > byte d;
    > }foo;


    > foo foo1;
    > x=sizeof(foo1);


    > What will be the value of x in different cases,
    > For One Byte Alignment
    > For Two Byte Alignment
    > For Four Byte Alignment
    > It will be nice if I get a good explaination too...Thanx


    It is entirely possible that x will be 1000 for each case. When
    calculating the sizeof of a struct, the C implementation has to
    take alignment issues into consideration. However, it isn't required
    to use alignment issues as the *only* reason for the sizeof.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "The large yellow ships hung in the sky in exactly the same way that bricks
    don't."
    - Douglas Adams
     
    Joona I Palaste, Nov 10, 2003
    #2
    1. Advertising

  3. Shashi

    Simon Biber Guest

    "Shashi" <> wrote:
    > Can somebody explain how the byte alignment for structures work,


    The insertion of padding bytes in structures is entirely up
    to each compiler and there is no rhyme nor reason specified
    by the C Standard. The only restriction is that there can
    be no padding before the first member of the structure. There
    can be any number of bytes skipped between each member and
    after the last member.

    > taking the following example and considering:
    > byte of 1 Byte
    > word of 2 Bytes
    > dword of 4 Bytes


    Good thing you specified that -- but remember that some
    implementations do not have an integer type with exactly
    2 bytes or exactly 4 bytes.

    > typedef struct
    > {
    > byte a;
    > word b;
    > dword c;
    > byte d;
    > } foo;
    >
    > foo foo1;
    > x=sizeof(foo1);
    >
    > What will be the value of x in different cases,


    All you can tell is that it will be at least 8 -- it
    could be any size greater than or equal to 8.

    --
    Simon.
     
    Simon Biber, Nov 10, 2003
    #3
  4. Shashi

    Eric Sosman Guest

    Shashi wrote:
    >
    > Can somebody explain how the byte alignment for structures work,
    > taking the following example and considering:
    > byte of 1 Byte
    > word of 2 Bytes
    > dword of 4 Bytes
    >
    > typedef struct
    > {
    > byte a;
    > word b;
    > dword c;
    > byte d;
    > }foo;
    >
    > foo foo1;
    > x=sizeof(foo1);
    >
    > What will be the value of x in different cases,
    > For One Byte Alignment
    > For Two Byte Alignment
    > For Four Byte Alignment
    > It will be nice if I get a good explaination too...Thanx


    The Standard that defines the C language says little
    about alignment. It notes that some implementations may
    find it advantageous to align certain data objects to
    certain address boundaries, and it permits implementations
    to insert extra "padding bytes" after struct elements in
    order to satisfy the alignment requirements. It can be
    deduced that the alignment for some object is a divisor
    of the object's size, but this is not explicitly stated.

    "One byte alignment" presumably means that a data
    object is properly aligned if its address is divisible by
    one, or in other words a data object can begin at any
    address. In this case there is no compelling reason for
    the implementation to insert any padding in a `foo' struct,
    and `sizeof(foo)' is probably 1+2+4+1 = 8 bytes.

    "Two byte alignment" presumably means that a data
    object is properly aligned if its address is divisible by
    two, but that objects smaller than two bytes need no
    alignment (because of the divisibility criterion). In
    this case there will probably be padding after `a' so that
    `b' and `c' can begin on even addresses, and more padding
    after `d' so the entire struct takes an even number of
    bytes (if its size were odd, you couldn't malloc() an
    array containing two of them). So the total is probably
    1+1+2+4+1+1 = 10 bytes.

    You should now be able to work out the four-byte case,
    and I'll leave you the pleasure of doing so (in case this
    is homework). The interesting thing about this case is
    that we know what alignment is required for `dword' and
    for `byte', but "four byte alignment" doesn't tell us how
    a `word' must be aligned. The two possibilities lead to
    two different likely arrangements of padding bytes, and
    you should try to find them both.

    Note that I've been saying "likely" arrangements and
    "probable" sizes, because the Standard grants quite a lot
    of freedom to the implementations in this matter. For
    example, an implementation is free to align a struct more
    strictly than any of its constituent elements, if that
    seems convenient to the implementors; the two-byte case
    probably gives a size of 10 bytes, but might plausibly
    yield 12 or 16 instead. The only way to be sure is to see
    what the implementation actually does -- and to realize
    that other implementations may do it differently.

    --
     
    Eric Sosman, Nov 10, 2003
    #4
  5. Shashi wrote:
    > Can somebody explain how the byte alignment for structures work,
    > taking the following example and considering:
    > byte of 1 Byte
    > word of 2 Bytes
    > dword of 4 Bytes
    >
    > typedef struct
    > {
    > byte a;
    > word b;
    > dword c;
    > byte d;
    > }foo;
    >
    > foo foo1;
    > x=sizeof(foo1);
    >
    > What will be the value of x in different cases,
    > For One Byte Alignment

    Offset field
    00 a
    01 b
    03 c
    07 d
    08


    > For Two Byte Alignment

    Offset field
    00 a
    01 {padding byte length 1}
    02 b
    04 c
    08 d
    09 {padding byte length 1}
    10


    > For Four Byte Alignment

    Offset field
    00 a
    01 {padding bytes length 3}
    04 b
    06 {padding bytes length 2}
    08 c
    12 d
    13 {padding bytes length 3}

    > It will be nice if I get a good explaination too...Thanx

    Many compilers will add extra bytes after a field to make
    it align to the next boundary. This is what alignment is
    with structures.

    Some compilers supply a pragma for "packing" structures,
    which means that no padding bytes are added. However, it
    may not access the fields correctly.

    Remember that the size of a structures may not be the
    sum of the size of its fields.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
     
    Thomas Matthews, Nov 10, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Rathnow

    JNI with 1 byte alignment crashes JVM

    Dave Rathnow, Nov 7, 2003, in forum: Java
    Replies:
    2
    Views:
    1,806
    Gordon Beaton
    Nov 7, 2003
  2. Ares Lagae

    Byte alignment in union

    Ares Lagae, Aug 25, 2003, in forum: C++
    Replies:
    21
    Views:
    4,241
    Michiel Salters
    Sep 1, 2003
  3. Marco

    byte alignment

    Marco, Jan 21, 2005, in forum: C++
    Replies:
    4
    Views:
    529
    EventHelix.com
    Jan 22, 2005
  4. gamja
    Replies:
    14
    Views:
    717
  5. Replies:
    1
    Views:
    406
    Lawrence Kirby
    Jul 6, 2005
Loading...

Share This Page