This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: why are we not using const?
- From: Chris Lattner <clattner at apple dot com>
- To: Dave Korn <dave dot korn at artimi dot com>
- Cc: "'Richard Guenther'" <richard dot guenther at gmail dot com>, "'Kaveh R. Ghazi'" <ghazi at caipclassic dot rutgers dot edu>, gdr at integrable-solutions dot net, gcc at gnu dot org, lopezibanez at gmail dot com, pinskia at physics dot uc dot edu
- Date: Thu, 29 Jun 2006 09:14:13 -0700
- Subject: Re: why are we not using const?
- References: <014d01c69b83$27ba9c40$a501a8c0@CAM.ARTIMI.COM>
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