Alignment of struct with flexible array member

  • Thread starter Hallvard B Furuseth
  • Start date
H

Hallvard B Furuseth

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?
 
C

Chris Torek

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.
 
B

Ben Pfaff

Chris Torek said:
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.
 
C

christian.bau

Ben said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top