Alignment

Discussion in 'C Programming' started by fishpond@invalid.com, Aug 19, 2007.

  1. Guest

    How to declare a variable guaranteed to have the strictest possible
    alignment?

    --
    The defense attorney was hammering away at the plaintiff:
    "You claim," he jeered, "that my client came at you with a broken bottle
    in his hand. But is it not true, that you had something in YOUR hand?"
    "Yes," the man admitted, "his wife. Very charming, of course,
    but not much good in a fight."
     
    , Aug 19, 2007
    #1
    1. Advertising

  2. <> wrote in message
    news:...
    > How to declare a variable guaranteed to have the strictest possible
    > alignment?
    >

    In practise, align to a double.
    eg

    union alignedint
    {
    double dummy;
    int x;
    }

    x will now have double alignement.

    In strictly correct terms, use compiler pragmas to achieve what you want.
    double isn't guaranteed to be the most strictly aligned type and there is no
    portable mechnaism for finding it.

    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
     
    Malcolm McLean, Aug 19, 2007
    #2
    1. Advertising

  3. wrote:
    > How to declare a variable guaranteed to have the strictest possible
    > alignment?


    Given that any object type can be contained in a structure or a union, and
    all structure and union types have the same alignment requirements, any
    structure or union type will do.

    However, why do you need this? There's very little in standard C that you
    can do with knowledge of alignment of non-dynamic objects. Even if you have
    a structure containing an array of four chars, which is properly aligned
    for an int, and sizeof(int)==4 on your platform, you're still not allowed
    to access this structure as were it an int.
     
    Harald van =?UTF-8?B?RMSzaw==?=, Aug 19, 2007
    #3
  4. Harald van Dijk wrote:
    > wrote:
    >> How to declare a variable guaranteed to have the strictest possible
    >> alignment?

    >
    > Given that any object type can be contained in a structure or a union, and
    > all structure and union types have the same alignment requirements, any
    > structure or union type will do.


    Sorry, this is of course incorrect. I was thinking of pointers to
    structures. The below is still true, though you'd need to have another way
    of making the structure properly aligned for it to be relevant.

    > However, why do you need this? There's very little in standard C that you
    > can do with knowledge of alignment of non-dynamic objects. Even if you
    > have a structure containing an array of four chars, which is properly
    > aligned for an int, and sizeof(int)==4 on your platform, you're still not
    > allowed to access this structure as were it an int.
     
    Harald van =?UTF-8?B?RMSzaw==?=, Aug 19, 2007
    #4
  5. pete Guest

    wrote:
    >
    > How to declare a variable guaranteed to have the strictest possible
    > alignment?


    Allocated objects have strictest alignment.

    Does it have to be a declared variable?

    --
    pete
     
    pete, Aug 19, 2007
    #5
  6. Harald van Dijk <> writes:
    > wrote:
    >> How to declare a variable guaranteed to have the strictest possible
    >> alignment?

    >
    > Given that any object type can be contained in a structure or a union, and
    > all structure and union types have the same alignment requirements, any
    > structure or union type will do.

    [...]

    Not true. All *pointers* to structure or union types have the same
    alignment requirements, but that refers only to the alignment of
    pointer objects, not to the alignment of what they point to. It's
    entirely possible for 'struct { int n; }' to have stricter alignment
    than 'struct { char c; }', for example.

    A union known to contain a member with the strictest possible
    alignment requirement would do the trick, but there's no portable way
    to determine this. A union containing members of types int, long,
    long long (if C99), float, double, long double, void*, and maybe a few
    other pointer types (including function pointer types) is very likely
    to work, but it's not guaranteed.

    If you want something guaranteed to be aligned for any type, use
    malloc(). Or check your system's documentation.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Aug 19, 2007
    #6
  7. "Malcolm McLean" <> writes:
    > <> wrote in message
    > news:...
    >> How to declare a variable guaranteed to have the strictest possible
    >> alignment?
    >>

    > In practise, align to a double.
    > eg
    >
    > union alignedint
    > {
    > double dummy;
    > int x;
    > }
    >
    > x will now have double alignement.


    What if long double has stricter alignment requirements than double?

    > In strictly correct terms, use compiler pragmas to achieve what you
    > want. double isn't guaranteed to be the most strictly aligned type and
    > there is no portable mechnaism for finding it.


    In strictly correct terms, (or, as I prefer to say, "correct terms")
    there is no portable way to do this; the language doesn't define any
    pragmas for this purpose.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Aug 19, 2007
    #7
  8. Guest

    On 19 Aug 2007 at 21:22, Keith Thompson wrote:
    > Harald van Dijk <> writes:
    >> wrote:
    >>> How to declare a variable guaranteed to have the strictest possible
    >>> alignment?

    >>
    >> Given that any object type can be contained in a structure or a union, and
    >> all structure and union types have the same alignment requirements, any
    >> structure or union type will do.

    > [...]
    >
    > Not true. All *pointers* to structure or union types have the same
    > alignment requirements, but that refers only to the alignment of
    > pointer objects, not to the alignment of what they point to. It's
    > entirely possible for 'struct { int n; }' to have stricter alignment
    > than 'struct { char c; }', for example.
    >
    > A union known to contain a member with the strictest possible
    > alignment requirement would do the trick, but there's no portable way
    > to determine this. A union containing members of types int, long,
    > long long (if C99), float, double, long double, void*, and maybe a few
    > other pointer types (including function pointer types) is very likely
    > to work, but it's not guaranteed.
    >
    > If you want something guaranteed to be aligned for any type, use
    > malloc(). Or check your system's documentation.


    It's hard to believe that something this simple can't be achieved in
    Standard C. I mean, if that's true, how would you ever get type punning
    going?

    >
    > --
    > Keith Thompson (The_Other_Keith)
    > <http://www.ghoti.net/~kst>
    > San Diego Supercomputer Center <*>
    > <http://users.sdsc.edu/~kst>
    > "We must do something. This is something. Therefore, we must do this."
    > -- Antony Jay and Jonathan Lynn, "Yes Minister"


    --
    I might have gone to West Point, but I was too proud to speak to a congressman.
    -- Will Rogers
     
    , Aug 19, 2007
    #8
  9. Keith Thompson wrote:
    > Harald van Dijk <> writes:
    >> wrote:
    >>> How to declare a variable guaranteed to have the strictest possible
    >>> alignment?

    >>
    >> Given that any object type can be contained in a structure or a union,
    >> and all structure and union types have the same alignment requirements,
    >> any structure or union type will do.

    > [...]
    >
    > Not true. All *pointers* to structure or union types have the same
    > alignment requirements, but that refers only to the alignment of
    > pointer objects, not to the alignment of what they point to. It's
    > entirely possible for 'struct { int n; }' to have stricter alignment
    > than 'struct { char c; }', for example.


    To clarify beyond the message I sent earlier: I used to believe that all
    structures must have the same alignment requirements. I have never read a
    satisfactory explanation of how the representation requirements for
    pointers to structures do not imply this, I accept that the intent was that
    different structures may have different alignment requirements, and all
    that has nothing to do with my first message in this thread, which was
    nothing more a slip of the mind unrelated to that old argument.

    Or, to shorten it somewhat: agreed.
     
    Harald van =?UTF-8?B?RMSzaw==?=, Aug 19, 2007
    #9
  10. Thad Smith Guest

    wrote:
    > How to declare a variable guaranteed to have the strictest possible
    > alignment?


    How to ask a question without a subject and verb?

    I assume you mean something like "how can I declare a variable
    guaranteed to have the strictest possible alignment?".

    To get alignment suitable to any type, dynamically allocate it.
    Otherwise, you can declare a union containing all the types that must be
    aligned properly.

    --
    Thad
     
    Thad Smith, Aug 19, 2007
    #10
  11. wrote:
    > On 19 Aug 2007 at 21:22, Keith Thompson wrote:
    >> If you want something guaranteed to be aligned for any type, use
    >> malloc(). Or check your system's documentation.

    >
    > It's hard to believe that something this simple can't be achieved in
    > Standard C. I mean, if that's true, how would you ever get type punning
    > going?


    Type punning isn't something you should do in code that's meant to be
    portable, and if the code isn't meant to be portable, it shouldn't matter
    as much that the behaviour is defined by your implementation rather than by
    the standard.
     
    Harald van =?UTF-8?B?RMSzaw==?=, Aug 19, 2007
    #11
  12. "Keith Thompson" <> wrote in message
    news:...
    > "Malcolm McLean" <> writes:
    >> <> wrote in message
    >> news:...
    >>> How to declare a variable guaranteed to have the strictest possible
    >>> alignment?
    >>>

    >> In practise, align to a double.
    >> eg
    >>
    >> union alignedint
    >> {
    >> double dummy;
    >> int x;
    >> }
    >>
    >> x will now have double alignement.

    >
    > What if long double has stricter alignment requirements than double?
    >

    Computer like things in units of powers of two.
    doubles are almost always 64 bits, in fact have to be if IEEE. long doubles
    are 80. So the alignment requirements for double are likely to be stricter.
    However you are right, in practise isn't the same as "by the standard". It
    might even be storing up trouble for the future to advise such a thing. I
    haven't made my mind up about this one.
    How about a new type. align_t ?

    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
     
    Malcolm McLean, Aug 19, 2007
    #12
  13. On Aug 19, 5:31 pm, wrote:
    > On 19 Aug 2007 at 21:22, Keith Thompson wrote:
    >
    >
    >
    > > Harald van D k <> writes:
    > >> wrote:
    > >>> How to declare a variable guaranteed to have the strictest possible
    > >>> alignment?

    >
    > >> Given that any object type can be contained in a structure or a union, and
    > >> all structure and union types have the same alignment requirements, any
    > >> structure or union type will do.

    > > [...]

    >
    > > Not true. All *pointers* to structure or union types have the same
    > > alignment requirements, but that refers only to the alignment of
    > > pointer objects, not to the alignment of what they point to. It's
    > > entirely possible for 'struct { int n; }' to have stricter alignment
    > > than 'struct { char c; }', for example.

    >
    > > A union known to contain a member with the strictest possible
    > > alignment requirement would do the trick, but there's no portable way
    > > to determine this. A union containing members of types int, long,
    > > long long (if C99), float, double, long double, void*, and maybe a few
    > > other pointer types (including function pointer types) is very likely
    > > to work, but it's not guaranteed.

    >
    > > If you want something guaranteed to be aligned for any type, use
    > > malloc(). Or check your system's documentation.

    >
    > It's hard to believe that something this simple can't be achieved in
    > Standard C. I mean, if that's true, how would you ever get type punning
    > going?


    If you want to pun types you can create a union with members of the
    types you want to participate in the punning. You are guaranteed that
    each member starts at the same address which is properly aligned for
    all the members, you also bypass the aliasing rules that would come
    into play with the alternative of casting through a pointer to a
    different type. The only thing you need to worry about is
    representation, if the punned type turns out to be a trap
    representation and you access it the behavior is undefined.

    Robert Gamble
     
    Robert Gamble, Aug 20, 2007
    #13
  14. Ark Khasin Guest

    wrote:
    > How to declare a variable guaranteed to have the strictest possible
    > alignment?
    >


    I guess the first question is, what's alignment? C99 3.2 says
    "requirement that objects of a particular type be located on storage
    boundaries with addresses that are particular multiples of a byte address."

    That's a motivational explanation (like a math explanation of a set) but
    not a definition: addresses are not numbers, and a multiple of a byte
    address is... what?

    Is it fair to say that all we can say (axiomatically) that
    - For a type T there is an alignment good for storing a T object, and
    - if T *p is an address good for storing a T object, then so is p+1?
    [And then whatever alignment hierarchy the standard defines.]

    I cannot imagine why one needs a strictest-aligned variable of static
    duration, but the following would work
    typedef union {
    char c;
    short s;
    int i;
    long l;
    float f;
    double d;
    long double ld;
    long long ll;
    //and whatever native types I missed
    } strict_align_t;
    strict_align_t myvar;

    Unsigned and complex stuff can be inferred to fit this alignment.

    -- Ark
     
    Ark Khasin, Aug 20, 2007
    #14
  15. Eric Sosman Guest

    Ark Khasin wrote:
    > wrote:
    >> How to declare a variable guaranteed to have the strictest possible
    >> alignment?
    >>

    >
    > I guess the first question is, what's alignment? C99 3.2 says
    > "requirement that objects of a particular type be located on storage
    > boundaries with addresses that are particular multiples of a byte address."
    >
    > That's a motivational explanation (like a math explanation of a set) but
    > not a definition: addresses are not numbers, and a multiple of a byte
    > address is... what?
    >
    > Is it fair to say that all we can say (axiomatically) that
    > - For a type T there is an alignment good for storing a T object, and
    > - if T *p is an address good for storing a T object, then so is p+1?
    > [And then whatever alignment hierarchy the standard defines.]
    >
    > I cannot imagine why one needs a strictest-aligned variable of static
    > duration, but the following would work
    > typedef union {
    > char c;
    > short s;
    > int i;
    > long l;
    > float f;
    > double d;
    > long double ld;
    > long long ll;
    > //and whatever native types I missed


    "Aye: There's the rub." -- H, P of D

    > } strict_align_t;
    > strict_align_t myvar;


    --
    Eric Sosman
    lid
     
    Eric Sosman, Aug 20, 2007
    #15
  16. Ian Collins Guest

    wrote:
    > On 19 Aug 2007 at 21:22, Keith Thompson wrote:
    >>
    >> If you want something guaranteed to be aligned for any type, use
    >> malloc(). Or check your system's documentation.

    >
    > It's hard to believe that something this simple can't be achieved in
    > Standard C. I mean, if that's true, how would you ever get type punning
    > going?
    >

    The fact that there isn't a simple standard way hits to the fact that
    specific alignments are seldom required. What's wrong or complicated
    with Keith's suggestion?

    --
    Ian Collins.
     
    Ian Collins, Aug 20, 2007
    #16
  17. Ark Khasin Guest

    Eric Sosman wrote:
    > Ark Khasin wrote:
    >>
    >> I cannot imagine why one needs a strictest-aligned variable of static
    >> duration, but the following would work
    >> typedef union {
    >> char c;
    >> short s;
    >> int i;
    >> long l;
    >> float f;
    >> double d;
    >> long double ld;
    >> long long ll;
    >> //and whatever native types I missed

    >
    > "Aye: There's the rub." -- H, P of D
    >
    >> } strict_align_t;
    >> strict_align_t myvar;

    >

    Of course. Will the addition of "void *v; const void *cv;" do it?

    And, taking off my assembler motley, may I ask again what is alignment?
    Is there some sort of conspiracy and addresses in fact /are/ some sort
    of numbers?
     
    Ark Khasin, Aug 20, 2007
    #17
  18. Ian Collins Guest

    Ark Khasin wrote:
    > Eric Sosman wrote:
    >> Ark Khasin wrote:
    >>>
    >>> I cannot imagine why one needs a strictest-aligned variable of static
    >>> duration, but the following would work
    >>> typedef union {
    >>> char c;
    >>> short s;
    >>> int i;
    >>> long l;
    >>> float f;
    >>> double d;
    >>> long double ld;
    >>> long long ll;
    >>> //and whatever native types I missed

    >>
    >> "Aye: There's the rub." -- H, P of D
    >>
    >>> } strict_align_t;
    >>> strict_align_t myvar;

    >>

    > Of course. Will the addition of "void *v; const void *cv;" do it?
    >

    Don't forget function pointers.

    --
    Ian Collins.
     
    Ian Collins, Aug 20, 2007
    #18
  19. Ark Khasin Guest

    Ian Collins wrote:
    > Ark Khasin wrote:
    >> Eric Sosman wrote:
    >>> Ark Khasin wrote:
    >>>> I cannot imagine why one needs a strictest-aligned variable of static
    >>>> duration, but the following would work
    >>>> typedef union {
    >>>> char c;
    >>>> short s;
    >>>> int i;
    >>>> long l;
    >>>> float f;
    >>>> double d;
    >>>> long double ld;
    >>>> long long ll;
    >>>> //and whatever native types I missed
    >>> "Aye: There's the rub." -- H, P of D
    >>>
    >>>> } strict_align_t;
    >>>> strict_align_t myvar;

    >> Of course. Will the addition of "void *v; const void *cv;" do it?
    >>

    > Don't forget function pointers.
    >

    Thanks! I've missed that C99: J.5.7 is informative, not normative.
     
    Ark Khasin, Aug 20, 2007
    #19
  20. Ark Khasin <> writes:
    [...]
    > I cannot imagine why one needs a strictest-aligned variable of static
    > duration, but the following would work
    > typedef union {
    > char c;
    > short s;
    > int i;
    > long l;
    > float f;
    > double d;
    > long double ld;
    > long long ll;
    > //and whatever native types I missed
    > } strict_align_t;
    > strict_align_t myvar;
    >
    > Unsigned and complex stuff can be inferred to fit this alignment.


    It's very likely to work, but it's certainly not guaranteed to work.
    There are arbitrarily many types, and any of them could theoretically
    have stricter alignment requirements than any of the types you've
    listed.

    If you're going to use that approach, you should include at least a
    few pointer types (void*, a struct pointer, and a function pointer).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Aug 20, 2007
    #20
    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. =?Utf-8?B?bWFudQ==?=

    Regd---Justify option in Alignment.

    =?Utf-8?B?bWFudQ==?=, Sep 23, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    456
    =?Utf-8?B?bWFudQ==?=
    Sep 23, 2005
  2. ALuPin

    Phase alignment

    ALuPin, May 17, 2004, in forum: VHDL
    Replies:
    0
    Views:
    593
    ALuPin
    May 17, 2004
  3. Sunil Menon

    Re: Help required for alignment problem

    Sunil Menon, Jun 26, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    822
    Sunil Menon
    Jun 26, 2003
  4. Anne
    Replies:
    0
    Views:
    655
  5. Anne
    Replies:
    0
    Views:
    529
Loading...

Share This Page