On Jun 29, 2006, at 6:51 AM, Dave Korn wrote:
That's cheating! You casted away const, it's a blatant aliasing
violation,
you deserve everything you get. The compiler is specifically
*allowed* to
assume you don't pull stunts like this *in order to* make const-
optimisation
possible and useful.
As others have said, casting away const (and then dereferencing the
pointer) is perfectly legal unless the underlying variable's
definition is const.
However, you don't even need to do this to illustrate why const
pointers can't be used for optimization:
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.
-Chris