This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Clarification anyone? -- was Re: Linux and aliasing?
- To: george at moberg dot com
- Subject: Re: Clarification anyone? -- was Re: Linux and aliasing?
- From: tim at franck dot Princeton dot EDU (Tim Hollebeek)
- Date: Tue, 8 Jun 1999 10:31:37 -0700 (EDT)
- Cc: egcs at egcs dot cygnus dot com
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