Question regarding memory alignment

Discussion in 'C Programming' started by somenath, Nov 12, 2007.

  1. somenath

    somenath Guest

    Hi All,

    I have one question regarding the alignment of pointer returned by
    malloc.

    In K&R2 page number 186 one union is used to enforce the alignment as
    mentioned bellow.
    typedef long Align;
    union header {
    struct {
    union header *ptr;
    unsigned size;
    }s;
    Align x;
    };
    typedef union header Header;

    In this author is saying that Align field is never used; it just
    forces each header to be aligned on a worst-case boundary.

    I am not able to understand how Align field forces the alignment ?
    According to my understanding alignment of data means storing the data
    in proper address as system needs . For example in some system integer
    should be stored such that starting address is divisible by power of
    2.
    But I am not getting how this kind of requirement is enforced by
    'Align' .

    Please provide some input on this topic.

    Regards,
    Somenath
    somenath, Nov 12, 2007
    #1
    1. Advertising

  2. somenath

    James Kuyper Guest

    somenath wrote:
    > Hi All,
    >
    > I have one question regarding the alignment of pointer returned by
    > malloc.
    >
    > In K&R2 page number 186 one union is used to enforce the alignment as
    > mentioned bellow.
    > typedef long Align;
    > union header {
    > struct {
    > union header *ptr;
    > unsigned size;
    > }s;
    > Align x;
    > };
    > typedef union header Header;
    >
    > In this author is saying that Align field is never used; it just
    > forces each header to be aligned on a worst-case boundary.
    >
    > I am not able to understand how Align field forces the alignment ?
    > According to my understanding alignment of data means storing the data
    > in proper address as system needs . For example in some system integer
    > should be stored such that starting address is divisible by power of
    > 2.
    > But I am not getting how this kind of requirement is enforced by
    > 'Align' .
    >
    > Please provide some input on this topic.


    The standard requires that a pointer to member of a union, when
    converted to a pointer to the union type, must compare equal to a
    pointer to the union itself, and vice versa. This basically implies that
    all of the members of the union start at the same memory location as the
    union itself (it actually falls a little short of proving it; but that
    was the intent, and it would require a deliberately perverse
    implementation to violate that intent while conforming to that requirement).

    Therefore, the union's starting location must be suitably aligned to
    also be a starting location for each of its members. Therefore, it's
    alignment requirement is likely to be the least common multiple (lcm) of
    the alignment requirement of each of it's members, and it is guaranteed
    to be a common multiple, even if it isn't the least one.
    In practice, alignment requirements are almost always powers of two, so
    the lcm of the alignment requirements is the same as the maximum
    alignment requirement.

    The intent of this code was that 'Align' would be a typedef of a type
    with the strictest alignment requirement, and that for this particular
    implementation, 'long' is one of those types. Therefore, including it in
    a union guarantees that the union satisfies the strictest alignment
    requirement.
    James Kuyper, Nov 12, 2007
    #2
    1. Advertising

  3. On Sun, 11 Nov 2007 19:00:07 -0800, somenath <>
    wrote:

    >
    >Hi All,
    >
    >I have one question regarding the alignment of pointer returned by
    >malloc.


    This section of the book is not talking about the standard malloc
    function. It is talking about the sample malloc function on page 187.

    >
    >In K&R2 page number 186 one union is used to enforce the alignment as
    >mentioned bellow.
    >typedef long Align;
    >union header {
    > struct {
    > union header *ptr;
    > unsigned size;
    > }s;
    >Align x;
    >};
    >typedef union header Header;
    >
    >In this author is saying that Align field is never used; it just
    >forces each header to be aligned on a worst-case boundary.
    >
    >I am not able to understand how Align field forces the alignment ?


    Consider a system with four-byte pointers, two-byte int, and
    eight-byte long, each aligned on a multiple of its length and long
    being the strictest. The union consists of two members, a struct s
    and a long x. The struct consists of two members, a pointer and an
    unsigned int. Obviously the struct must be aligned on a four-byte
    boundary its sizeof must be a multiple of four. However, there is at
    least one type that must be eight-byte aligned. To insure that any
    object of type union header is so aligned, the member x of type long
    is added to the union. Since x and s start at the same address (at
    the beginning of the union), x forces the entire union to be aligned
    on a multiple of eight (which is also a multiple of four to satisfy s)
    with a sizeof that is also a multiple of eight. They do this because
    the address this sample malloc will return is going to be one byte
    beyond the end of an instance of this union type. This will insure
    that the sample malloc complies with the interface of the standard
    malloc in that the returned address is properly aligned for any type
    of object.

    >According to my understanding alignment of data means storing the data
    >in proper address as system needs . For example in some system integer
    >should be stored such that starting address is divisible by power of
    >2.
    >But I am not getting how this kind of requirement is enforced by
    >'Align' .
    >
    >Please provide some input on this topic.


    Remember, the book is 20 years old. On a modern system, long long or
    long double may have a stricter alignment requirement than long.


    Remove del for email
    Barry Schwarz, Nov 13, 2007
    #3
    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. Peter van Merkerk

    Re: memory alignment?

    Peter van Merkerk, Aug 1, 2003, in forum: C++
    Replies:
    1
    Views:
    395
    David Cattarin
    Aug 1, 2003
  2. Thomas Matthews

    Re: memory alignment?

    Thomas Matthews, Aug 1, 2003, in forum: C++
    Replies:
    0
    Views:
    371
    Thomas Matthews
    Aug 1, 2003
  3. Andrew Koenig

    Re: memory alignment?

    Andrew Koenig, Aug 1, 2003, in forum: C++
    Replies:
    1
    Views:
    329
    Karl Heinz Buchegger
    Aug 1, 2003
  4. Replies:
    3
    Views:
    336
    Keith Thompson
    Dec 17, 2006
  5. Regarding malloc memory alignment

    , Dec 6, 2007, in forum: C Programming
    Replies:
    10
    Views:
    3,846
    schat
    Mar 19, 2010
Loading...

Share This Page