Another question wrt storage alignment.

Q

qarnos

Hi, all.

A while ago I posted a question about storage alignment (thread
"Calculating storage alignment") and your answers were very helpful,
so I was hoping you could help me out again.

My question is straighforward:

Given two structs,

struct foo
{
void * a;
unsigned b;
char c;
};

struct bar
{
unsigned b;
char c;
};

Is there any guarantee that the alignment of "c" in both structures,
relative to "b", will be the same?

I'd like to think "yes", given that struct foo could be written as:

struct foo
{
void * a;
bar bc;
};

In this case I would expect the same alignment, although I have a
niggling feeling that a DS9K machine may disagree.

You input would be appreciated.

Regards,

Darren
 
J

jameskuyper

qarnos said:
Hi, all.

A while ago I posted a question about storage alignment (thread
"Calculating storage alignment") and your answers were very helpful,
so I was hoping you could help me out again.

My question is straighforward:

Given two structs,

struct foo
{
void * a;
unsigned b;
char c;
};

struct bar
{
unsigned b;
char c;
};

Is there any guarantee that the alignment of "c" in both structures,
relative to "b", will be the same?

No. Different compilers can insert different amounts of padding
between b and c. I can't come up reason why they might, but since it's
not guaranteed, I'd recommend not relying upon it. That's the kind of
thing that the offsetof() macro is for.

I'd like to think "yes", given that struct foo could be written as:

struct foo
{
void * a;
bar bc;
};

In this case I would expect the same alignment, although I have a
niggling feeling that a DS9K machine may disagree.

It would.
 
J

jameskuyper

jameskuyper said:
qarnos wrote: ....

No. Different compilers can insert different amounts of padding
between b and c.

While perfectly true, that's irrelevant to your question. What I
should have said was:

No. A compiler is free to insert different amounts of padding between
b and c in the two structs.
 
E

Eric Sosman

qarnos said:
Hi, all.

A while ago I posted a question about storage alignment (thread
"Calculating storage alignment") and your answers were very helpful,
so I was hoping you could help me out again.

My question is straighforward:

Given two structs,

struct foo
{
void * a;
unsigned b;
char c;
};

struct bar
{
unsigned b;
char c;
};

Is there any guarantee that the alignment of "c" in both structures,
relative to "b", will be the same?

I'd like to think "yes", given that struct foo could be written as:

struct foo
{
void * a;
bar bc;
};

In this case I would expect the same alignment, although I have a
niggling feeling that a DS9K machine may disagree.

You input would be appreciated.

There's no such guarantee in the general case. There *is*
a different guarantee that amounts to almost the same thing,
for practical compilers: If two or more structs with a "common
initial sequence" appear in the same union and one of those is
currently stored in the union, the common initial elements of
any of the others can be accessed just as if they had been
stored instead. For example,

struct s1 { int a; int b; double c; };
struct s2 { int x; int y; char z; };
union { struct s1 one; struct s2 two; } u;
u.one.a = 42;
u.one.b = 57;
u.one.c = 2.718;
/* Now the union holds a struct s1 value */
assert (u.two.x == 42);
assert (u.two.y == 57);
/* ... but u.two.z is off-limits */

This can only work if u.one.b and u.two.y have the same offsets,
that is, if the first two elements of the structs are laid out
identically.

The guarantee only applies if the structs appear in a union,
hence not to "free-floating" structs. But (1) the compiler can
usually not tell that two "free" structs in a.c might not also
appear in the same union in the separately-compiled b.c, and (2)
the compiler has little incentive to do things strangely. So it's
not a guarantee, but it's a "Nudge, nudge, wink, wink, say no more."
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top