struct and union alignment

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • S.Tobias

    struct and union alignment


    I would like to check if I understand the following excerpt correctly:

    6.2.5#26 (Types):
    All pointers to structure types shall have the same representation and
    alignment requirements as each other. All pointers to union types shall
    have the same representation and alignment requirements as each other.

    Does it mean that *all* structure (or union) types have the same
    alignment?
    Eg. type
    struct { char c; }
    and
    struct { long double ldt[11]; }
    have the same alignment requirements?


    If the answer to the above is "yes", then it means that:

    1. Alignment of any struct (union) must be maximum or its multiple.
    (Alignment of a struct (union) can't be less restrictive than that
    of its members[*]. Since any (object) type may be a member, at
    least one struct (union) must have at least that type's alignment.
    Since all struct (union) types have the same alignment,
    it follows that all must meet the the maximum one.)

    2. Alignment requirements for all structs _and_ unions are the same.
    (Since you can have a struct as a member of a union, and a union as a
    member of a struct, it follows that their alignments must be the same).
    [*] During google search I have learned that a type's alignments
    may differ in a struct and outside. Here I rather mean the least
    restrictive alignment requirement for a type that has to be met
    for a particular architecture.


    --
    Stan Tobias
    sed 's/[A-Z]//g' to email
  • Keith Thompson

    #2
    Re: struct and union alignment

    "S.Tobias" <sNOiSPAMt@amu. edu.pl> writes:[color=blue]
    > I would like to check if I understand the following excerpt correctly:
    >
    > 6.2.5#26 (Types):
    > All pointers to structure types shall have the same representation and
    > alignment requirements as each other. All pointers to union types shall
    > have the same representation and alignment requirements as each other.
    >
    > Does it mean that *all* structure (or union) types have the same
    > alignment?
    > Eg. type
    > struct { char c; }
    > and
    > struct { long double ldt[11]; }
    > have the same alignment requirements?[/color]

    No, it's referring to the alignment of the pointer, not the alignment
    of the struct or union.

    --
    Keith Thompson (The_Other_Keit h) kst-u@mib.org <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.

    Comment

    • Flash Gordon

      #3
      Re: struct and union alignment

      On 23 Sep 2004 19:10:38 GMT
      "S.Tobias" <sNOiSPAMt@amu. edu.pl> wrote:
      [color=blue]
      >
      > I would like to check if I understand the following excerpt correctly:
      >
      > 6.2.5#26 (Types):
      > All pointers to structure types shall have the same representation
      > and alignment requirements as each other. All pointers to union
      > types shall have the same representation and alignment requirements
      > as each other.
      >
      > Does it mean that *all* structure (or union) types have the same
      > alignment?
      > Eg. type
      > struct { char c; }
      > and
      > struct { long double ldt[11]; }
      > have the same alignment requirements?[/color]

      Yes. Think about what what has to be done to implement pointers to
      anonymous structs. When the compiler is compiling a piece of code that
      increments a pointer to an anonymous struct all it knows is that it is a
      pointer to a struct, so how else could this have been done?
      [color=blue]
      > If the answer to the above is "yes", then it means that:
      >
      > 1. Alignment of any struct (union) must be maximum or its multiple.
      > (Alignment of a struct (union) can't be less restrictive than that
      > of its members[*]. Since any (object) type may be a member, at
      > least one struct (union) must have at least that type's alignment.
      > Since all struct (union) types have the same alignment,
      > it follows that all must meet the the maximum one.)[/color]

      Yes.
      [color=blue]
      > 2. Alignment requirements for all structs _and_ unions are the same.
      > (Since you can have a struct as a member of a union, and a union as
      > a member of a struct, it follows that their alignments must be the
      > same).[/color]

      Sounds logical to me.
      [color=blue]
      >[*] During google search I have learned that a type's alignments
      > may differ in a struct and outside. Here I rather mean the least
      > restrictive alignment requirement for a type that has to be met
      > for a particular architecture.[/color]

      The alignment inside a struct is at least as strict as outside, since
      you can take a pointer to an element in a struct. However, yes, I
      can't think of anything that would prevent the compiler from padding
      things out further.
      --
      Flash Gordon
      Sometimes I think shooting would be far too good for some people.
      Although my email address says spam, it is real and I read it.

      Comment

      • S.Tobias

        #4
        Re: struct and union alignment

        Keith Thompson <kst-u@mib.org> wrote:[color=blue]
        > "S.Tobias" <sNOiSPAMt@amu. edu.pl> writes:[color=green]
        > > 6.2.5#26 (Types):
        > > All pointers to structure types shall have the same representation and
        > > alignment requirements as each other. All pointers to union types shall
        > > have the same representation and alignment requirements as each other.
        > >
        > > Does it mean that *all* structure (or union) types have the same
        > > alignment?[/color][/color]
        [color=blue]
        > No, it's referring to the alignment of the pointer, not the alignment
        > of the struct or union.[/color]

        What's the difference?

        Strictly speaking, (complete) _types_ have alignment requirements.
        As I understand it, when we say "a pointer to TYPE has alignment
        requirements", we mean exactly same thing as: "TYPE has alignment
        requirements"; I don't see any reason to differentiate between these two.

        (I have also considered "pointer to struct" type (ie.: struct s *)
        alignment requirements, but this doesn't make sense in context of
        the quoted part of the Standard.)
        [color=blue]
        > --
        > Keith Thompson (The_Other_Keit h) kst-u@mib.org <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.[/color]

        --
        Stan Tobias
        sed 's/[A-Z]//g' to email

        Comment

        • Keith Thompson

          #5
          Re: struct and union alignment

          Flash Gordon <spam@flash-gordon.me.uk> writes:[color=blue]
          > On 23 Sep 2004 19:10:38 GMT
          > "S.Tobias" <sNOiSPAMt@amu. edu.pl> wrote:[color=green]
          >> I would like to check if I understand the following excerpt correctly:
          >>
          >> 6.2.5#26 (Types):
          >> All pointers to structure types shall have the same representation
          >> and alignment requirements as each other. All pointers to union
          >> types shall have the same representation and alignment requirements
          >> as each other.
          >>
          >> Does it mean that *all* structure (or union) types have the same
          >> alignment?
          >> Eg. type
          >> struct { char c; }
          >> and
          >> struct { long double ldt[11]; }
          >> have the same alignment requirements?[/color]
          >
          > Yes. Think about what what has to be done to implement pointers to
          > anonymous structs. When the compiler is compiling a piece of code that
          > increments a pointer to an anonymous struct all it knows is that it is a
          > pointer to a struct, so how else could this have been done?[/color]

          When 6.2.5 discusses the alignment requirements of various types, it's
          clear that it's referring to the alignment of objects of the type
          itself. It uses similar wording in 6.2.5p26, so I think it's
          referring to the alignment of a pointer object, not the alignment of
          what it points to.

          I don't think this is a problem for pointers to anonymous structs.
          You can't create a pointer value that points to an anonymous struct
          without knowing what the actual type is. Similarly, you can't
          increment a pointer to an anonymous struct without knowing the
          struct's size.

          I believe that an implementation in which pointer values are simple
          byte addresses, small structs (struct { char c; }) have, say, 1-byte
          alignment, and larger structs have, say, 4-byte alignment could be
          conforming. Can you think of a concrete example where this would
          cause problems?

          [...]
          [color=blue][color=green]
          >>[*] During google search I have learned that a type's alignments
          >> may differ in a struct and outside. Here I rather mean the least
          >> restrictive alignment requirement for a type that has to be met
          >> for a particular architecture.[/color]
          >
          > The alignment inside a struct is at least as strict as outside, since
          > you can take a pointer to an element in a struct. However, yes, I
          > can't think of anything that would prevent the compiler from padding
          > things out further.[/color]

          Assume in the following that sizeof(int) == 4.

          On some hardware, an int can be accessed more efficiently if it's
          aligned on a 4-byte boundary, but can still be accessed if it's merely
          byte-aligned. An implementation could consistently choose to align
          all declared int objects on 4-byte boundary, but use byte alignment
          for struct members that are of type int (to save space). Or vice
          versa.

          I'm not saying that this would be a sensible thing to do, but it's
          legal.

          I suppose we're talking about two different meanings of "alignment
          requirements": the alignment the compiler chooses to use for a given
          type, and the alignment that's actually required by the hardware. The
          former needs to be at least as strict as the latter, but it needn't be
          the same.

          --
          Keith Thompson (The_Other_Keit h) kst-u@mib.org <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.

          Comment

          • S.Tobias

            #6
            Re: struct and union alignment

            Flash Gordon <spam@flash-gordon.me.uk> wrote:[color=blue]
            > On 23 Sep 2004 19:10:38 GMT
            > "S.Tobias" <sNOiSPAMt@amu. edu.pl> wrote:[/color]
            [color=blue][color=green]
            > > Does it mean that *all* structure (or union) types have the same
            > > alignment?[/color][/color]
            [color=blue]
            > Yes. Think about what what has to be done to implement pointers to
            > anonymous structs. When the compiler is compiling a piece of code that
            > increments a pointer to an anonymous struct all it knows is that it is a
            > pointer to a struct, so how else could this have been done?[/color]

            I'm sorry, but I can't see how can you define a pointer to anonymous
            struct in the first place (since that struct has no name). And how
            that could relate to alignment issues (incrementing a pointer has
            at most to do with type size, not alignment).

            [ C Std doesn't define anonymous structs. I've found somewhere the
            anonymous structs are structs "declared" within other structs/unions,
            that don't have a tag, object nor typedef name. ]

            [color=blue][color=green]
            > >[*] During google search I have learned that a type's alignments
            > > may differ in a struct and outside.[/color][/color]
            [color=blue]
            > The alignment inside a struct is at least as strict as outside, since
            > you can take a pointer to an element in a struct. However, yes, I
            > can't think of anything that would prevent the compiler from padding
            > things out further.[/color]

            I read someone saying that alignment requirements inside a struct might
            be even less restrictive than outside (I've lost that post, but when I
            find it I'll post it here). The explanation was like this: for example
            a 32-bit int may have 2-byte alignment requirement, but if it's on 4-byte
            boundary it's accessed faster. So an implementation might have 4-byte
            int alignment outside structs, but for better packing reasons 2-byte
            alignment inside.

            --
            Stan Tobias
            sed 's/[A-Z]//g' to email

            Comment

            • Keith Thompson

              #7
              Re: struct and union alignment

              "S.Tobias" <sNOiSPAMt@amu. edu.pl> writes:[color=blue]
              > Keith Thompson <kst-u@mib.org> wrote:[color=green]
              >> "S.Tobias" <sNOiSPAMt@amu. edu.pl> writes:[color=darkred]
              >> > 6.2.5#26 (Types):
              >> > All pointers to structure types shall have the same representation and
              >> > alignment requirements as each other. All pointers to union types shall
              >> > have the same representation and alignment requirements as each other.
              >> >
              >> > Does it mean that *all* structure (or union) types have the same
              >> > alignment?[/color][/color]
              >[color=green]
              >> No, it's referring to the alignment of the pointer, not the alignment
              >> of the struct or union.[/color]
              >
              > What's the difference?
              >
              > Strictly speaking, (complete) _types_ have alignment requirements.
              > As I understand it, when we say "a pointer to TYPE has alignment
              > requirements", we mean exactly same thing as: "TYPE has alignment
              > requirements"; I don't see any reason to differentiate between these two.[/color]

              It's just like the difference between the size of a pointer and the
              size of what it points to.

              For example, a pointer object might require 4-byte alignment, whereas
              a char object requires only 1-byte alignment. When the standard says
              void* and char* have to have the same alignment requirements, the
              intent is that they're interchangeable as arguments to functions.
              Requiring 1-byte alignment for char* but only 4-byte alignment for
              void* could break this.

              Look at the other references to alignment requirements in 6.2.5; they
              clearly refer to the alignment of the type itself. 6.2.5#26 uses
              similar wording, so it refers to the alignment of the pointers
              themselves. (Alignment requirements for character types are discussed
              elsewhere.)

              --
              Keith Thompson (The_Other_Keit h) kst-u@mib.org <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.

              Comment

              • Mabden

                #8
                Re: struct and union alignment

                "Flash Gordon" <spam@flash-gordon.me.uk> wrote in message
                news:pstb22x2mb .ln2@brenda.fla sh-gordon.me.uk...[color=blue]
                > On 23 Sep 2004 19:10:38 GMT
                > "S.Tobias" <sNOiSPAMt@amu. edu.pl> wrote:
                >[color=green]
                > > Does it mean that *all* structure (or union) types have the same
                > > alignment?
                > > Eg. type
                > > struct { char c; }
                > > and
                > > struct { long double ldt[11]; }
                > > have the same alignment requirements?[/color][/color]

                Unions also have the caveat that they be aligned according to the most
                restrictive item in the union. So a struct {char a; double z;} could
                have a different alignment than union {char a; double z;}.

                --
                Mabden


                Comment

                • Barry Schwarz

                  #9
                  Re: struct and union alignment

                  On 23 Sep 2004 23:51:44 GMT, "S.Tobias" <sNOiSPAMt@amu. edu.pl> wrote:
                  [color=blue]
                  >Keith Thompson <kst-u@mib.org> wrote:[color=green]
                  >> "S.Tobias" <sNOiSPAMt@amu. edu.pl> writes:[color=darkred]
                  >> > 6.2.5#26 (Types):
                  >> > All pointers to structure types shall have the same representation and
                  >> > alignment requirements as each other. All pointers to union types shall
                  >> > have the same representation and alignment requirements as each other.
                  >> >
                  >> > Does it mean that *all* structure (or union) types have the same
                  >> > alignment?[/color][/color]
                  >[color=green]
                  >> No, it's referring to the alignment of the pointer, not the alignment
                  >> of the struct or union.[/color]
                  >
                  >What's the difference?
                  >
                  >Strictly speaking, (complete) _types_ have alignment requirements.
                  >As I understand it, when we say "a pointer to TYPE has alignment
                  >requirements ", we mean exactly same thing as: "TYPE has alignment
                  >requirements "; I don't see any reason to differentiate between these two.[/color]

                  Actually, this is informally speaking, not strictly speaking.
                  Informally, some talk about aligning a pointer when we really mean
                  aligning the *VALUE* assigned to the pointer which is equivalent to
                  aligning the object the pointer points to.

                  Strictly speaking, when we talk about aligning a pointer we mean
                  aligning the pointer itself.

                  For example, on my system all pointers require four-byte alignment
                  while doubles require eight-byte alignment. A pointer to double need
                  only be aligned on a four-byte boundary but its value better be
                  aligned on an eight-byte boundary.
                  [color=blue]
                  >
                  >(I have also considered "pointer to struct" type (ie.: struct s *)
                  >alignment requirements, but this doesn't make sense in context of
                  >the quoted part of the Standard.)[/color]

                  Actually it does. If we have a number of different structs, some with
                  one-byte alignment, some with two, some with four, and some with
                  eight, a set of pointers to these different structs all have the same
                  alignment (four-byte on my system) and representation, even though the
                  objects they point to (and therefore the values they contain) have
                  different requirements.

                  Just as note, this is what allows a struct type to contain a member
                  which is a pointer to the same struct type (think linked list). At
                  the time the member is being processed by the compiler, the alignment
                  requirements of the struct are not yet known (the struct is still
                  incomplete) but the alignment requirements of the pointer are known so
                  the compiler can decide how much padding is needed.


                  <<Remove the del for email>>

                  Comment

                  • Barry Schwarz

                    #10
                    Re: struct and union alignment

                    On 23 Sep 2004 19:10:38 GMT, "S.Tobias" <sNOiSPAMt@amu. edu.pl> wrote:
                    [color=blue]
                    >
                    >I would like to check if I understand the following excerpt correctly:
                    >
                    >6.2.5#26 (Types):
                    > All pointers to structure types shall have the same representation and
                    > alignment requirements as each other. All pointers to union types shall
                    > have the same representation and alignment requirements as each other.
                    >
                    >Does it mean that *all* structure (or union) types have the same
                    >alignment?
                    >Eg. type
                    > struct { char c; }
                    >and
                    > struct { long double ldt[11]; }
                    >have the same alignment requirements?[/color]

                    No. It is only talking about the pointers themselves and not about
                    the values assigned to the pointers or the objects the pointers point
                    to.
                    [color=blue]
                    >
                    >
                    >If the answer to the above is "yes", then it means that:
                    >
                    >1. Alignment of any struct (union) must be maximum or its multiple.
                    > (Alignment of a struct (union) can't be less restrictive than that
                    > of its members[*]. Since any (object) type may be a member, at
                    > least one struct (union) must have at least that type's alignment.
                    > Since all struct (union) types have the same alignment,
                    > it follows that all must meet the the maximum one.)[/color]

                    Since the answer is no, the conclusion is unwarranted.
                    [color=blue]
                    >
                    >2. Alignment requirements for all structs _and_ unions are the same.
                    > (Since you can have a struct as a member of a union, and a union as a
                    > member of a struct, it follows that their alignments must be the same).[/color]

                    Ditto.
                    [color=blue]
                    >
                    >[*] During google search I have learned that a type's alignments
                    >may differ in a struct and outside. Here I rather mean the least
                    >restrictive alignment requirement for a type that has to be met
                    >for a particular architecture.[/color]

                    I don't think so in standard C. It may be possible with some compiler
                    specific extensions (e.g., some have an option to eliminate padding).


                    <<Remove the del for email>>

                    Comment

                    • Keith Thompson

                      #11
                      Re: struct and union alignment

                      "Mabden" <mabden@sbc_glo bal.net> writes:[color=blue]
                      > "Flash Gordon" <spam@flash-gordon.me.uk> wrote in message
                      > news:pstb22x2mb .ln2@brenda.fla sh-gordon.me.uk...[color=green]
                      >> On 23 Sep 2004 19:10:38 GMT
                      >> "S.Tobias" <sNOiSPAMt@amu. edu.pl> wrote:
                      >>[color=darkred]
                      >> > Does it mean that *all* structure (or union) types have the same
                      >> > alignment?
                      >> > Eg. type
                      >> > struct { char c; }
                      >> > and
                      >> > struct { long double ldt[11]; }
                      >> > have the same alignment requirements?[/color][/color]
                      >
                      > Unions also have the caveat that they be aligned according to the most
                      > restrictive item in the union. So a struct {char a; double z;} could
                      > have a different alignment than union {char a; double z;}.[/color]

                      Both unions and structs have to be aligned at least as strictly as
                      their most strictly aligned member. Both struct {char a; double z;}
                      and union {char a; double z;} have to be aligned in a way that allows
                      the 'z' member to be accessed. (This can require padding between
                      members, and possibly after the last member, for a struct; any padding
                      in a union occurs after each member, since all members have the same
                      offset.)

                      --
                      Keith Thompson (The_Other_Keit h) kst-u@mib.org <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.

                      Comment

                      • Christian Kandeler

                        #12
                        Re: struct and union alignment

                        S.Tobias wrote:
                        [color=blue]
                        > I'm sorry, but I can't see how can you define a pointer to anonymous
                        > struct in the first place (since that struct has no name).[/color]

                        struct {
                        int a;
                        int b;
                        } *pointer_to_ano nymous_struct;


                        Christian

                        Comment

                        • S.Tobias

                          #13
                          Re: struct and union alignment

                          Keith Thompson <kst-u@mib.org> wrote:
                          [color=blue]
                          > For example, a pointer object might require 4-byte alignment, whereas
                          > a char object requires only 1-byte alignment. When the standard says
                          > void* and char* have to have the same alignment requirements, the
                          > intent is that they're interchangeable as arguments to functions.[/color]

                          Right.
                          extern char *pc;
                          void free(void *p);
                          free(pc); //correct

                          (Note: we don't need same representation requirement in the above
                          example. This requirement is necessary eg. for variadic arguments:
                          printf("%p", pc);
                          - no cast to void* required.)
                          [color=blue]
                          > Requiring 1-byte alignment for char* but only 4-byte alignment for
                          > void* could break this.[/color]

                          No. I see no reason to require same alignment for types void* and char*,
                          because in function arguments they're passed by *value* (think conversion).
                          Similarly, long and char do not have to have same alignment, but we may
                          always use them in arguments "interchangeabl y":
                          extern long l;
                          extern char c;
                          int f_char(unsigned char c);
                          int f_long(unsigned long l);
                          f_char(l); //both calls correct
                          f_long(c);

                          [color=blue]
                          > Look at the other references to alignment requirements in 6.2.5; they
                          > clearly refer to the alignment of the type itself. 6.2.5#26 uses
                          > similar wording, so it refers to the alignment of the pointers
                          > themselves. (Alignment requirements for character types are discussed
                          > elsewhere.)[/color]

                          I have looked in the entire text. The Standard uses the word "alignment"
                          in three ways: type (or object) alignment; pointer value alignment
                          (it uses words: "value", "result", "address"); or pointer alignment
                          (the way we discuss now).

                          The last manner is also found in: 6.3.2.3#7 (Pointers) and 7.20.3#1
                          (Memory management functions). See for yourself: in both cases "pointer
                          alignment" refers to the pointer *value*, not pointer type.


                          Barry Schwarz <schwarzb@deloz .net> wrote:[color=blue]
                          > On 23 Sep 2004 23:51:44 GMT, "S.Tobias" <sNOiSPAMt@amu. edu.pl> wrote:[/color]
                          [color=blue][color=green]
                          > >Strictly speaking, (complete) _types_ have alignment requirements.
                          > >As I understand it, when we say "a pointer to TYPE has alignment
                          > >requirements ", we mean exactly same thing as: "TYPE has alignment
                          > >requirements "; I don't see any reason to differentiate between these two.[/color][/color]
                          [color=blue]
                          > Actually, this is informally speaking, not strictly speaking.
                          > Informally, some talk about aligning a pointer when we really mean
                          > aligning the *VALUE* assigned to the pointer which is equivalent to
                          > aligning the object the pointer points to.[/color]
                          [color=blue]
                          > Strictly speaking, when we talk about aligning a pointer we mean
                          > aligning the pointer itself.[/color]
                          [color=blue][color=green]
                          > >(I have also considered "pointer to struct" type (ie.: struct s *)
                          > >alignment requirements, but this doesn't make sense in context of
                          > >the quoted part of the Standard.)[/color][/color]

                          So this is actually what you and Keith think the Standard refers to!
                          So the fight is over "strictly" and "informally ". I think that
                          the Standard in all cases means "informally ".

                          [color=blue]
                          > Just as note, this is what allows a struct type to contain a member
                          > which is a pointer to the same struct type (think linked list). At
                          > the time the member is being processed by the compiler, the alignment
                          > requirements of the struct are not yet known (the struct is still
                          > incomplete) but the alignment requirements of the pointer are known so
                          > the compiler can decide how much padding is needed.[/color]

                          While this might be a problem, I agree, this is an internal matter
                          of the implementation how it solves this (pointer type alignment and
                          padding before it). The Standard is a contract between an implementor
                          and a programmer. There is nothing a programmer can gain from
                          knowing that all pointer-to-struct types have same alignment and
                          thanks to it the compiler can easily construct a structure type.
                          I just think your argument misses the point.

                          OT:
                          The compiler might construct a struct type in several passes, each
                          time adjusting the padding before the pointer member.
                          This process must end, because there exists a maximum alignment.


                          ========

                          My arguments:


                          6.2.5#26 (extended quote, to get more context)
                          A pointer to void shall have the same representation and alignment
                          requirements as a pointer to a character type.39) Similarly,
                          pointers to qualified or unqualified versions of compatible types
                          shall have the same representation and alignment requirements. All
                          pointers to structure types shall have the same representation and
                          alignment requirements as each other. All pointers to union types
                          shall have the same representation and alignment requirements
                          as each other. Pointers to other types need not have the same
                          representation or alignment requirements.

                          39) The same representation and alignment requirements are meant
                          to imply interchangeabil ity as arguments to functions, return
                          values from functions, and members of unions.

                          Rationale:
                          [...] A pointer to void must have the same representation
                          and alignment as a pointer to char; the intent of this rule is
                          to allow existing programs that call library functions such as
                          memcpy and free to continue to work.

                          Remark: It is obvious the Standard means "pointers to (structure types)",
                          not "(pointers to structure) types". This follows from English grammar
                          rules, but please don't ask me for chapter and verse! :-)

                          The first part the Std talks about the "compatibil ity" of void* and char*
                          types. Here it definitely refers to alignment of pointer *values*.
                          This is required for conversions between void* and char* to be well
                          defined. Of course, the Std cannot say directly about requirements
                          for the types they point to, because void is not a complete type (but
                          this is the intention if we forget it for a while and treat void as an
                          integer type with size one, but not compatible with char).
                          (OTOH the Std could formally define alignment of void type to be equal
                          to that of char type.)

                          The Standard cannot refer to the alignment of _types_ void* and char*.
                          If it did, it would only concern conversions between void** and char**.
                          The only standard functions with that type are strto?() family and friends
                          (and then we should add same requirement for wchar_t* to be consistent).
                          The rationale mentions only functions like free() or memcpy(), ie. ones
                          that probably historically had int free(char*) prototypes before the
                          type void was introduced. Here, the alignment compatibility between
                          the _values_ of type void* and char* is essential - IOW it means the
                          resolution of void* and char* pointer types is the highest and both
                          are equivalent in this matter.

                          Then, after the word "Similarly" , the Standard talks about alignment
                          of pointers to compatible types, struct and unions, and other types in
                          the same manner. There is no reason to believe it uses similar wording
                          in a different manner this time.

                          There is no advantage in guaranteeing same alignment for "pointers
                          to incompatible types" types (at least I can't think of any).
                          All of them can be stored as void* if need be.

                          --
                          Stan Tobias
                          sed 's/[A-Z]//g' to email

                          Comment

                          • S.Tobias

                            #14
                            Re: [OT] struct and union alignment

                            Christian Kandeler <christian.kand eler@hob.de_inv alid> wrote:[color=blue]
                            > S.Tobias wrote:[/color]
                            [color=blue][color=green]
                            > > I'm sorry, but I can't see how can you define a pointer to anonymous
                            > > struct in the first place (since that struct has no name).[/color][/color]
                            [color=blue]
                            > struct {
                            > int a;
                            > int b;
                            > } *pointer_to_ano nymous_struct;[/color]

                            OIC, didn't think of that.

                            But then again, what would be a use for that? Since
                            this is not a struct object definition, there are no objects
                            of type pointer_to_anon ymous_struct points to.

                            Mmmm, could it be:
                            pointer_to_anon ymous_struct = malloc(sizeof *pointer_to_ano nymous_struct);
                            ?
                            But what is the point in having an anonymous struct then?

                            Could you supply a real-life example?

                            --
                            Stan Tobias
                            sed 's/[A-Z]//g' to email

                            Comment

                            • Simon Stienen

                              #15
                              Re: [OT] struct and union alignment

                              "S.Tobias" <sNOiSPAMt@amu. edu.pl> wrote:[color=blue]
                              > Christian Kandeler <christian.kand eler@hob.de_inv alid> wrote:[color=green]
                              >> S.Tobias wrote:[/color]
                              >[color=green][color=darkred]
                              >>> I'm sorry, but I can't see how can you define a pointer to anonymous
                              >>> struct in the first place (since that struct has no name).[/color][/color]
                              >[color=green]
                              >> struct {
                              >> int a;
                              >> int b;
                              >> } *pointer_to_ano nymous_struct;[/color]
                              >
                              > OIC, didn't think of that.
                              >
                              > But then again, what would be a use for that? Since
                              > this is not a struct object definition, there are no objects
                              > of type pointer_to_anon ymous_struct points to.
                              >
                              > Mmmm, could it be:
                              > pointer_to_anon ymous_struct = malloc(sizeof *pointer_to_ano nymous_struct);
                              > ?
                              > But what is the point in having an anonymous struct then?
                              >
                              > Could you supply a real-life example?[/color]

                              Maybe something like:

                              int main() {
                              uint32 x = 0x5678abcd;
                              struct {
                              uint16 hiword;
                              uint16 loword;
                              } *wordsinlong = &x;
                              printf("Hiword: %x\nLoword: %x",
                              words_in_long->hiword,
                              words_in_long->loword);
                              return 0;
                              }

                              --
                              Simon Stienen <http://dangerouscat.ne t> <http://slashlife.de>
                              »What you do in this world is a matter of no consequence,
                              The question is, what can you make people believe that you have done.«
                              -- Sherlock Holmes in "A Study in Scarlet" by Sir Arthur Conan Doyle

                              Comment

                              Working...