[EMAIL PROTECTED] (Kai Henningsen) writes:

> ian@airs.com (Ian Lance Taylor)  wrote on 20.01.06 in <[EMAIL PROTECTED]>:
> 
> > When dealing with unions, you can take pointers to different fields in
> > the unions.  If the fields have different types, these pointers can
> > have non-conflicting alias sets.  Therefore within a single union the
> > same memory can be read or written by different pointers.  This is
> > considered to be invalid--a valid program is required to always access
> > the memory within the union in the same type, except if you access the
> > memory via the union type itself (this permission being a gcc
> > extension).
> 
> void test(void)
> {
>         union { int i; double d; } u;
>         int *ip;
>         double *dp;
>         int ii;
>         double dd;
> 
>         ip = &u.i;
>         *ip = 15;
>         ii = *ip;
>         dp = &u.d;
>         *dp = 1.5;
>         dd = *dp;
>         printf("ii=%d dd=%f\n", ii, dd);
> }
> 
> So you're saying this function is not valid?

That function is valid.  I expressed myself badly above.  What I
should have said is that if you have a pointer to a field in the
union, and you do a read, a valid program must have previously done a
write using a pointer to the same field.  A write using a pointer to
one field followed by a read using a pointer to another field is
invalid.

Ian

Reply via email to