int G;
void foo(const int *P1) {
G = *P1 + 1;
}
int bar() {
int tmp = G;
foo(&G);
return G-tmp;
}
bar returns 1, not 0, and there is no pointer casting happening.
Well, G is known to escape anyway here. Even worse is this:
-- f1.c --
extern int G;
void foo(const int *P1) {
int a = *P1;
G = *P1 + 1; /* optimizable even for non-const int *P1 */
return *P1 - a; /* non-optimizable anyway */
}
-- f2.c --
int G;
int bar() {
return foo(&G);
}
where there is not even the possibility to optimize *P1 in foo. While
compiling f1.c, the compiler does not even know that G escapes and must
assume the worse.
It's a different story for static variables or for -fwhole-program, but
then the compiler (as for the second load from *P1) can optimize the
third load anyway, independent of whether P1 is const or not.
Paolo