Mail Archives: djgpp/2001/07/20/06:15:05
Lets Go Canes <letsgonhlcanes AT yahoo DOT com> wrote:
> Hi all.
> What you are doing is called "aliasing". Many compilers would give you
> the same result even if you removed the "const" qualifier.
No. At least not any correct compilers conforming to Standard C in its
traditional version dated 1989/1990, and I strongly believe those to
be the overwhelming majority of compilers in use these days. Standard
C requires that all such side effects, whether by pointer aliasing or
other means, be respected correctly, to a certain extent: no two such
side effects are allowed to affect the same object between any two
"sequence points". A compiler that doesn't account for such side
effects is broken beyond recognition. The program at hand does have
undefined behaviour, but not because of aliasing.
C++ is no different from C, in this respect, to the best of my
knowledge.
In fact, this aliasing issue is one of the reasons C programs can
sometimes not be optimized as perfectly as some programmers want them
to be -- it's one of the main reasons some people still insist coding
in language like FORTRAN. That's why, in the new C99 standard, a
keyword was introduced that allows the programmer to state explicitly
that a particular variable is not aliassed by any pointer: "restrict".
> Basically, the compiler doesn't know that "k" is a pointer to the
> same memory occupied by "j" (i.e., "k*" is an alias for "j"), and so
> if it already has the value of "j" in a register, it will use it,
> even though you just changed the value of "j" via the "k*" alias.
If it did that, it'd be a severly broken compiler.
--
Hans-Bernhard Broeker (broeker AT physik DOT rwth-aachen DOT de)
Even if all the snow were burnt, ashes would remain.
- Raw text -