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]

Re: Clarification anyone? -- was Re: Linux and aliasing?


Tim Hollebeek <tim@franck.Princeton.EDU> writes:
>
>Let me try to give a simple definition, since the discussion so far
>has been dominated by technical details.  It has nothing to do with
>pointer casts, though they figure prominently in lots of code that
>violates the ANSI assumptions.  The "main idea" is that when you
>declare something to be of a particular type, that's what it's type
>actually is.  When you access that object, you must access it through
>pointers with the correct type (pointer to whatever) or a pointer type
>designed to represent raw memory (pointer to char).
>
>This is actually suprisingly useful information for a compiler.
>Consider something like:
>
>void count_and_set(blah *foo, double *z, double v) {
>    while (*z) {
>        foo->bar->count++;
>        *z++ = p;
>    }
>}
>
>(&foo->bar) is obviously a loop invariant and can be hoisted out of
>the loop, but not in "traditional" C!  z might point into foo
>somewhere, so the write through z might change the value of foo->bar.
>ANSI C says such code is nonsense, and if you really meant to play
>fast and loose with the type system, you should use char *'s,
>memcpy's, etc.

Does not sound too bad so far - so can someone give an example of 
the code that would be broken by that?

So ... just to check my understanding.

I assume unions are exempt from this?

e.g.

typedef union
 {float f;
  struct
   {
#if BIG_ENDIAN
    unsigned sgn:1;
    unsigned exp:8;
    unsigned mant:23;
#else
    unsigned mant:23;
    unsigned exp:8;
    unsigned sgn:1;
#endif
   } s;
 } ieee_sp;




What is proposed is that any mem-assign can be considered to "clobber"
any value of the same type, and an assign via a char * to clobber anything ?

And thus problem is that if I do this:

int a;
*((short *) &a) = 123;

Then 'a' is not considered clobbered?

Even in the same function?

-- 
Nick Ing-Simmons <nik@tiuk.ti.com>
Via, but not speaking for: Texas Instruments Ltd.


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