This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Clarification anyone? -- was Re: Linux and aliasing?
- To: george at moberg dot com
- Subject: Re: Clarification anyone? -- was Re: Linux and aliasing?
- From: "Martin v. Loewis" <martin at mira dot isdn dot cs dot tu-berlin dot de>
- Date: Wed, 9 Jun 1999 00:02:49 +0200
- Cc: egcs at egcs dot cygnus dot com
- References: <XFMail.990608094539.george@moberg.com>
> 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