Christian Bau said:
Not really.
The compiler must make things "work" _if_ two structs _are_ in fact
members of the same union and _if_ this is known when the code is
compiled, and in no other case. If you give the compiler a source file
containing main () and some other functions, and the compiler can deduce
that no function outside that file are actually called, then it is
clearly allowed to use different offsets for both structs.
I believe that whether or not the compiler can deduce that no function
outside the single-translation-unit program is called is irrelevant.
First, 6.2.7 p1 doesn't say translation units in the same executable;
it just says translation units. Second, there are lots of ways a
structure value might be transmitted between different executables,
eg,
- writing bytes out to a file and another executable reading
them in;
- storing a value in shared memory;
- transmitting bytes over a pipe or a socket;
- just leaving a value in memory somewhere and expecting the
next program to pick it up;
- for a more subtle example - the value of an 'offsetof'
macro call might be stored by one executable and used
by another.
The C standard makes (some) guarantees about program execution, but it
seems silly to think it makes guarantees *only* about program
execution. There also are guarantees about what representations are
used in (some) data types. I think most people reading sections
6.2.5, 6.2.6, 6.2.7 and 6.3 would conclude that guarantees are made
about the representations of compatible types (of structs) in
different translation units, regardless of whether they were ever
bound into a single executable. In response to that, do you have
anything to offer other than just an assertion to the contrary?
Saying "... it is clearly allowed ..." without giving any supporting
statements isn't very convincing.
And having the same layout doesn't guarantee defined behavior, as the
compiler can often assume that pointers to different structs don't point
to the same memory.
Certainly having the same layout doesn't guarantee defined behavior in
all cases. But I think it *does* guarantee defined behavior in *some*
cases that otherwise would be undefined.
I acknowledge the point about the compiler being allowed to make
assumptions for pointers pointing to different types. I saw that
point in an earlier posting of yours, and it's a good one. What
I'm asking about, however, is something different.