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: "Dave Korn" <dave dot korn at artimi dot com>
- To: "'Richard Guenther'" <richard dot guenther at gmail dot com>, "'Kaveh R. Ghazi'" <ghazi at caipclassic dot rutgers dot edu>
- Cc: <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 14:51:43 +0100
- Subject: RE: why are we not using const?
On 29 June 2006 14:44, Richard Guenther wrote:
> But with C language constructs you cannot assume that an object
> passed to a function via a const pointer is not modified. So, there
> is no real "const" regarding to objects pointed to. Consider
>
> void foo(const int *i)
> {
> int *k = (int *)i;
> *k = 0;
> }
> int bar(void)
> {
> int i = 1;
> foo(&i);
> return i;
> }
>
> should return 0, not 1.
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.
cheers,
DaveK
--
Can't think of a witty .sigline today....