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?


george@moberg.com writes ...
> 
> Can someone post or otherwise direct me to a clear statement of what the new
> aliasing rules are?
> 
> 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.

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.

Hope this helps anyone who is trying to follow along.

-Tim


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