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: Gabriel Dos Reis <gdr at integrable-solutions dot net>
- 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>, <gcc at gnu dot org>, <lopezibanez at gmail dot com>, <pinskia at physics dot uc dot edu>
- Date: 29 Jun 2006 19:36:11 +0200
- Subject: Re: why are we not using const?
- References: <014d01c69b83$27ba9c40$a501a8c0@CAM.ARTIMI.COM>
"Dave Korn" <dave.korn@artimi.com> writes:
| 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.
Not from the C language point of view.
-- Gaby