This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: ISO Aliasing rules question
- From: "Joseph S. Myers" <jsm28 at cam dot ac dot uk>
- To: Michael Matz <matz at suse dot de>
- Cc: <law at redhat dot com>, <gcc at gcc dot gnu dot org>
- Date: Sat, 22 Feb 2003 00:30:13 +0000 (GMT)
- Subject: Re: ISO Aliasing rules question
On Fri, 21 Feb 2003, Michael Matz wrote:
> Hi,
>
> On Fri, 21 Feb 2003 law at redhat dot com wrote:
>
> > struct s1 { double d; };
> > struct s2 { double d; };
> >
> > double f(struct s1 *a, struct s2 *b)
> > {
> > a->d = 1.0;
> > return b->d + 1.0;
> > }
> >
> > int main()
> > {
> > struct s1 a;
> > a.d = 0.0;
> > if (f (&a, (struct s2 *)&a) != 2.0)
> > abort ();
> > return 0;
> > }
> >
> >
> > Does this code produce undefined behavior according to ISO standard,
> > particularly in regards to aliasing issues.
>
> I asked myself also repeatedly similar things. Is 'a->d' a whole object
> or not? If yes, then 'a->d' and 'b->d' alias because they have the same
> type. I not, i.e. they are only part of the objects *a and *b, then they
> can not alias, because as you notice 'struct s1' and 'struct s2' are not
> compatible types as defined in 6.2.7 #1.
The basic "what is an object?" question is discussed in Nick Maclaren's
discussion with a title something like that, posted to the WG14 reflector
some time ago, required reading for anyone contemplating such issues; ask
him for a copy if you don't have one.
In this case, a (in main) has declared type (so effective type) struct s1,
so _if_ the structure is the relevant object then access through the type
struct s2 would be undefined. But if you declared a as double, and then
cast &a to each pointer type, then I think all accesses in f would be
legitimate: an object of effective type double is accessed through
structure types with an element of that type.
DR#236 discusses some problems with the type-based aliasing rules that are
far from resolved. A proper resolution of such problems requires the
underlying issues with the concept of an object to be resolved first.
--
Joseph S. Myers
jsm28 at cam dot ac dot uk