473,390 Members | 1,728 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,390 software developers and data experts.

Alignment of struct with flexible array member

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
3 3854
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

36
by: Bhalchandra Thatte | last post by:
I am allocating a block of memory using malloc. I want to use it to store a "header" structure followed by structs in my application. How to calculate the alignment without making any assumption...
18
by: Panchal V | last post by:
I want to access a variable length record in C, the format is as follows : +---+---+-----------+ | A | L | D A T A | +---+---+-----------+ A - Some Data (1 BYTE) L - Length the Data that...
15
by: fix | last post by:
Hi all, I am writing a program using some structs, it is not running and I believe it is because there's some memory leak - the debugger tells me that the code causes the problem is in the malloc...
22
by: David Rasmussen | last post by:
If I have struct foo { char bar; long baz; }; on many platforms, padding and alignment is taking place to get baz to start at an even address or an address that 4 divides. So baz is not
13
by: aegis | last post by:
The following was mentioned by Eric Sosman from http://groups.google.com/group/comp.lang.c/msg/b696b28f59b9dac4?dmode=source "The alignment requirement for any type T must be a divisor of...
10
by: haomiao | last post by:
I want to implement a common list that can cantain any type of data, so I declare the list as (briefly) --------------------------------------- struct list { int data_size; int node_num;...
27
by: arkmancn | last post by:
Any comments? thanks. Jim
6
by: Francois Grieu | last post by:
Hello, I'm asking myself all kind of questions on allocating an array of struct with proper alignment. Is the following code oorrect ? I'm most interested by the statement t =...
66
by: Why Tea | last post by:
typedef struct some_struct { int i; short k, int m; char s; } some_struct_t; Assuming 16 bit or 32-bit alignment, can I assume that s always gets 4 or 8 bytes of allocation due to padding
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.