Quote:
>I recently read that pointers can cause aliasing and prevent optimization by the
>compiler. I don't understanding what aliasing is. I'd appreciate if someone can
>explain it to me.
'Aliasing' is the computer science version of the philosopher's age-old
question 'is the morning star the same as the evening star?' Of course,
modern astronomy says 'yes, they are both Venus'. Shakespeare also put
it well: 'a rose by any other name would smell as sweet'.
Aliasing is tightly coupled with the 'problem of _identity_ in object-
oriented languages'. A good operational description of 'identity' is
found by making changes to an object by means of one description, and
seeing whether the object referenced by the other description also
changes. If yes, the objects are identical; if no, the objects are
not identical.
Pointers in C are excellent ways to achieve aliasing. In this
context, they are _good_, because they are the semantics you want.
However, aliasing interferes with _caching_, which is a making of a
_copy_ of an object in a more convenient and efficient place (instead
of _moving_ the object to the more convenient place, but still keeping
only the one copy). Thus, the semantics could be affected if someone
changes the original object, which will not be reflected in the cached
_copy_.
A particularly convenient and efficient place for an object is a
machine _register_, and compilers attempt to cache objects there as
much as possible. Aliasing interferes with the compiler's ability to
cache values in this manner, and may thus slow down a potential
program.
One of the major reasons for incorporating hardware caches on modern
architectures is to properly and efficiently handle _aliasing_ of
values which the compiler is not able to cache in registers itself.
I wouldn't worry very much about trying to avoid aliasing to help the
compiler, unless you are doing something totally egregious. (You are
in good company, because most C programs _alias_ things like crazy.)
Hardware caches are getting better and better, so the performance hit
for an object in the cache relative to one in a register isn't that
bad for most applications.