473,468 Members | 1,369 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Alignment in structure

Hello,

I have a question about the layout of the fields in a structure. I have
read
in a paper that if two structures contain an initial sequence of
fields,
all of which have compatible types, then the offsets of the
corresponding
fields in initial sequence are guaranteed to be the same. For example,
with

typedef struct {
int a;
int b;
char c;
} s1;

typedef struct {
int a;
int b;
int d[2];
} s2;

s1 t1;
s2 t2;

then the fields t1.b and t2.b have the same offset. I know that,
according
to the ISO specification (ISO/IEC 9899:1999, §6.5.2.3), this happens
when
we consider two elements of type s1 and s2 in a union, for example :

union {
s1 t1;
s2 t2
} a;

Is it true when t1 and t2 are not fields of a union ?

Thanks !

Regards.

Xavier

Nov 15 '05 #1
5 2064
MeJohn wrote:
Hello,

I have a question about the layout of the fields in a structure. I have
read
in a paper that if two structures contain an initial sequence of
fields,
all of which have compatible types, then the offsets of the
corresponding
fields in initial sequence are guaranteed to be the same. For example,
with

typedef struct {
int a;
int b;
char c;
} s1;

typedef struct {
int a;
int b;
int d[2];
} s2;

s1 t1;
s2 t2;

then the fields t1.b and t2.b have the same offset. I know that,
according
to the ISO specification (ISO/IEC 9899:1999, §6.5.2.3), this happens
when
we consider two elements of type s1 and s2 in a union, for example :

union {
s1 t1;
s2 t2
} a;

Is it true when t1 and t2 are not fields of a union ?


The special guarantee of 6.5.2.3 only applies to structs
that are members of the same union. However, a compiler would
need to be unbelievably perverse to let union membership (sings:
"Look for the union label ...") change the struct layout. Even
in the absence of your `union ... a', it would be hard for the
compiler to prove to itself that s1 and s2 don't appear as union
members in another source file, perhaps in a source file that
hasn't even been written yet.

Even if the layout of the initial struct members is the same,
though, you're not out of the woods. Let's take a different pair
of struct types to make the matter clearer:

typedef struct {
char a;
char b;
char c;
} s3;

typedef struct {
char a;
char b;
double d;
} s4;

Now, we know from the language definition that offsetof(s3,a)
and offsetof(s4,a) are both zero. We're also going to suppose
that offsetof(s3,b) == offsetof(s4,b), even though this isn't
truly guaranteed. However, this does not mean that we can
use an s4* to access the a and b elements of an s3! The two
struct types may well have different alignment requirements,
and the compiler is entitled to assume that a valid s4* points
to a location that meets the alignment requirement for an
actual s4 instance. It might then generate different code to
access the more stringently-aligned data: "Instead of fetching
the two bytes individually, I'll use this clever longword-
aligned instruction to fetch them both at one blow and then
use fast register-to-register instructions to split 'em apart."
If instead you point your s4* at what is actually an s3 instance
that might not be s4-aligned, the s4-dependent generated code
may malfunction.

(By the way, the above is not a merely theoretical concern:
I once tracked down a bug that stemmed from exactly this cause.)

Advice: Avoid the practice if you can reasonably do so.
You usually can, perhaps at the expense of introducing another
level of struct, as in

typedef struct { char a, b; } preamble;

typedef struct {
preamble p;
char c;
} s5;

typedef struct {
preamble p;
double d;
} s6;

You can now take an s5* or s6*, convert it to a preamble*, and
use the latter to access the a and b members with complete
safety. (You must actually do the conversion, though: for the
alignment reasons mentioned above, it would not be safe to use
the s6* itself to access the p.a and p.b members of an s5, or
vice versa.)

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 15 '05 #2
Thank you for you answer.

To sum up, if you consider two pointers p1 and p2 of type s1* and s2*
respectively, the cast p1 = (s1*) p2 is safe if and only if s1 is a
"prefix" of s2 (modulo the compatibility of the types of the fields).

Nov 15 '05 #3


MeJohn wrote:
Thank you for you answer.

To sum up, if you consider two pointers p1 and p2 of type s1* and s2*
respectively, the cast p1 = (s1*) p2 is safe if and only if s1 is a
"prefix" of s2 (modulo the compatibility of the types of the fields).


No; I wrote that the conversion is *not* safe because
s1 and s2 may have different alignment requirements. (Also,
we're not talking about the entirety of s1 being a prefix of
s2, but of the two struct types sharing a common prefix
which might not encompass all of either type.)

--
Er*********@sun.com

Nov 15 '05 #4
MeJohn wrote on 01/09/05 :
I have a question about the layout of the fields in a structure.
You should not. The layout is poorly define in C. What is defined is

- The first field has the same address than the structure.
- The order of the fields is the same than the definition.

The rest depends on the implementation (size, gaps...). In other words
don't rely on it. Never.
I have
read
in a paper that if two structures contain an initial sequence of
fields,
all of which have compatible types, then the offsets of the
corresponding
fields in initial sequence are guaranteed to be the same. For example,
with
Only the first element.
typedef struct {
int a;
int b;
char c;
} s1;

typedef struct {
int a;
int b;
int d[2];
} s2;

s1 t1;
s2 t2;
here, t1.a and t2.a are 'compatible'. For the other fields, there is no
guarantee.
then the fields t1.b and t2.b have the same offset. I know that,
according
to the ISO specification (ISO/IEC 9899:1999, §6.5.2.3), this happens
when
we consider two elements of type s1 and s2 in a union, for example :

union {
s1 t1;
s2 t2
} a;

Is it true when t1 and t2 are not fields of a union ?


Same issue. Only the offsets of the first elements are guaranteed to be
0.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"There are 10 types of people in the world today;
those that understand binary, and those that dont."
Nov 15 '05 #5
Do you have an example that doesn't use any pragma, or any other
instruction to the compiler relative to the alignments ? Thanks a lot !

Nov 15 '05 #6

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...
67
by: S.Tobias | last post by:
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...
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...
5
by: pt | last post by:
Hi, i am wonderng what is faster according to accessing speed to read these data structure from the disk in c/c++ including alignment handling if we access it on little endian system 32 bits...
3
by: Bill Pursell | last post by:
I have a program that does most of its work traversing a bunch of lists. The lists contain a void *, and I spent some time today replacing the void *'s with a copy of the data at the end of the...
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;...
4
by: myfavdepo | last post by:
Hi friends, i am having some trouble in my prog. with struct member alignment. I have two different static libraries that i use, each with "struct member alignment" set to 8 bytes. In my...
4
by: junky_fellow | last post by:
Can somebody please tell me about the structure alignment rules ? What I found was that on my system (cygwin running on PC, size of int=4 sizeof long=4, size of long long = 8) the cygwin compiler...
55
by: fishpond | last post by:
How to declare a variable guaranteed to have the strictest possible alignment? -- The defense attorney was hammering away at the plaintiff: "You claim," he jeered, "that my client came at you...
5
by: xmllmx | last post by:
Please forgive me for cross-posting. I've post this to microsoft.publoc.vc.mfc. But I can't get any response. Maybe only MFC- related topics are cared there. To begin with code: union XXX {...
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
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.