Alignment of struct with flexible array member

Discussion in 'C Programming' started by Hallvard B Furuseth, Dec 13, 2006.

  1. to find the required alignment of a struct, I've used

    #include <stddef.h>

    struct Align_helper {
    char dummy;
    struct S align;
    };

    enum { S_alignment = offsetof(struct Align_helper, align) };

    gcc -std=c99 -pedantic says that is invalid if S has a flexible array
    member:

    struct S {
    int a;
    char c[];
    };

    Is there some way to find the alignment of that struct?

    --
    Hallvard
     
    Hallvard B Furuseth, Dec 13, 2006
    #1
    1. Advertising

  2. Hallvard B Furuseth

    Chris Torek Guest

    In article <>,
    Hallvard B Furuseth <> wrote:
    >to find the required alignment of a struct, I've used
    >
    > #include <stddef.h>
    >
    > struct Align_helper {
    > char dummy;
    > struct S align;
    > };
    >
    > enum { S_alignment = offsetof(struct Align_helper, align) };
    >
    >gcc -std=c99 -pedantic says that is invalid if S has a flexible array
    >member:
    >
    > struct S {
    > int a;
    > char c[];
    > };
    >
    >Is there some way to find the alignment of that struct?


    Interesting. This might be more of a comp.std.c question: I assume
    gcc is unhappy because a flexible array member (FAM) must, for
    obvious reasons, be the last member of a "struct". Here the FAM
    is the last element of an embedded sub-struct, and the sub-struct
    is the last member of the top-level struct. So the FAM *is* in fact
    the last member, but this can only be determined by "deep" inspection
    instead of "shallow" inspection. (We can continue embedding a
    struct within a struct within a struct to produce any desired
    inspection depth:

    struct S100 { struct S99 { struct S98 { ... struct S01 {
    struct S00 { size_t fam_size; char fam[]; } lastelem;
    } lastelem; } lastelem; ... } lastelem };

    The inspection depth required here is 101.)

    Does C99 really require that FAMs be discoverable by what I have
    called "shallow" inspection? If not, it seems to me that gcc is
    wrong here, and your trick should work; if so, gcc is right.
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://web.torek.net/torek/index.html
    Reading email is like searching for food in the garbage, thanks to spammers.
     
    Chris Torek, Dec 13, 2006
    #2
    1. Advertising

  3. Hallvard B Furuseth

    Ben Pfaff Guest

    Chris Torek <> writes:

    > Does C99 really require that FAMs be discoverable by what I have
    > called "shallow" inspection? If not, it seems to me that gcc is
    > wrong here, and your trick should work; if so, gcc is right.


    C99 is pretty clear that flexible array members must be
    discoverable by shallow inspection:

    Constraints

    2 A structure or union shall not contain a member with incomplete
    or function type (hence, a structure shall not contain an
    instance of itself, but may contain a pointer to an instance
    of itself), except that the last member of a structure with
    more than one named member may have incomplete array type;
    such a structure (and any union containing, possibly
    recursively, a member that is such a structure) shall not be
    a member of a structure or an element of an array.

    --
    "It would be a much better example of undefined behavior
    if the behavior were undefined."
    --Michael Rubenstein
     
    Ben Pfaff, Dec 13, 2006
    #3
  4. Ben Pfaff wrote:
    > C99 is pretty clear that flexible array members must be
    > discoverable by shallow inspection:
    >
    > Constraints
    >
    > 2 A structure or union shall not contain a member with incomplete
    > or function type (hence, a structure shall not contain an
    > instance of itself, but may contain a pointer to an instance
    > of itself), except that the last member of a structure with
    > more than one named member may have incomplete array type;
    > such a structure (and any union containing, possibly
    > recursively, a member that is such a structure) shall not be
    > a member of a structure or an element of an array.


    So it is legal to have a union which has a member which is a union
    which has a member which is a union which has a member which is a
    struct whose last element has incomplete array type!

    So any difficulty in parsing cannot be the reason why a struct with
    such a struct as last member is explicitly not allowed.
     
    christian.bau, Dec 13, 2006
    #4
    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. Adam Warner

    Flexible array member + variable length array

    Adam Warner, Feb 3, 2005, in forum: C Programming
    Replies:
    10
    Views:
    800
    S.Tobias
    Feb 10, 2005
  2. DevarajA

    flexible array member

    DevarajA, Nov 12, 2005, in forum: C Programming
    Replies:
    2
    Views:
    350
    DevarajA
    Nov 13, 2005
  3. Replies:
    2
    Views:
    394
  4. Replies:
    4
    Views:
    346
    Jim Langston
    Dec 28, 2006
  5. Tuan  Bui
    Replies:
    14
    Views:
    476
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page