Clarification anyone? -- was Re: Linux and aliasing?
Martin v. Loewis
martin@mira.isdn.cs.tu-berlin.de
Tue Jun 8 15:06:00 GMT 1999
> I've been following this thread from the beginning, but I don't have the
> ANSI/ISO standard for C, and I'd like to know more about what kind of code is
> conformant and what isn't.
Tim already answered that, but then also explained what optimizations
the compiler can do. Here's just the rule, in different wording:
<rule>
Any access to an object of some type through a pointer of a different,
incompatible type is invalid (unless the different type is char*).
</rule>
(Look Ma, I didn't say 'alias':)
<example>
So if you have
struct foo *my_foo = get_some_foo();
struct bar *my_bar = (struct bar*)my_foo;
then writing
my_bar->some_date = 42;
is incorrect (actually, undefined). However, writing
struct foo *my_foo2 = (struct foo*)my_bar;
my_foo2->some_data_inside_foo = -42;
is fine: the object behind my_foo2 is really of type 'struct foo'.
</example>
Hope this clarifies it,
Martin
More information about the Gcc
mailing list