By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,853 Members | 1,809 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,853 IT Pros & Developers. It's quick & easy.

Alignment of struct with flexible array member

P: n/a
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
Dec 13 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
In article <hb**************@bombur.uio.no>,
Hallvard B Furuseth <h.**********@usit.uio.nowrote:
>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 (4039.22'N, 11150.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.
Dec 13 '06 #2

P: n/a
Chris Torek <no****@torek.netwrites:
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
Dec 13 '06 #3

P: n/a
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.

Dec 14 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.