This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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....


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]