This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: ISO Aliasing rules question


Michael Matz <matz at suse dot de> writes:

> Hi,
> 
> On Fri, 21 Feb 2003, Mark Mitchell wrote:
> 
> > This is tricky.  In general, you're right -- the structure of the type
> > doesn't matter, only the name.
> 
> Yes, but he's accessing a member, not the whole struct.  And that access
> has type 'double'.  6.5 #7 only speaks about accessing "an object".  Is
> somewhere defined, that a->d is not such an object, but merely a part of a
> bigger one (and b->d too, so 6.5 #7 would apply to the whole struct, which
> indeed can't alias).

Yes, both '*a' and 'a->d' are objects.  When you write '(*a).d', you
access *a then you access d, and 'a->d' is the same as '(*a).d'.

> Hmm, I can construct a funny example:
> 
> struct s1 {int i1, i2; };
> struct s2 {int i1, i2; };
> int g(struct s1 *a, struct s2* b);
> int f()
> {
>   struct s1 * p = malloc (sizeof (struct s1));
>   // P1
>   p->i1 = 2;
>   // P2
>   ((struct s2 *)p)->i2 = 3;
>   // P3
>   g (p, (struct s2* )p);
> }
> 
> As per 6.5 #4 (effective type of objects);
> At P1 *p does not have an effective type yet.  At P2 the object
> 'p->i1' does have effective type double.  But what is the effective type
> of *p? 

It's 'struct s1'.  "If a value is stored into an object ... the type
of the lvalue becomes the effective type of the object ... for
subsequent accesses that do not modify the stored value."

> If you can answer that, then which type has *p at P3?

At P3, it has 'struct s2': "for all other accesses, the effective
type... is the type used".  (The sentence I quoted earlier don't apply
because this is an access which does "modify the stored value" of *p.)

-- 
- Geoffrey Keating <geoffk at geoffk dot org>


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]