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: Compiling GCC With a C++ Compiler (g++)


> -----Original Message-----
> From: Andreas Schwab 
> Sent: 13 October 2004 14:27

> Gabriel Dos Reis writes:
> 
> > If you can a strcture with const-qualified field, you can modify any
> > part, except that field.  (I'm talking of C here).
> 
> Yes, I'm also talking about C.  And in C you can have const qualified
> lvalues that ultimatively denote modifiable objects.  I can't 
> see how it
> makes a difference that in this case it is declared as an 
> array, since the
> underlying object is clearly writable.

  Yes, but it's more complex than just whether the memory area pointed to is
writeable or not.

  If you pass a pointer to a struct as an argument to a function, and there
is a field in the struct that is declared const, the compiler is liable to
assume that the value of that field cannot be modified by the called
function and cache it in registers or stack slots across the function call,
or perhaps make assumptions about known values in the field, that would
become invalid if the callee casts away the constness and modifies the
field.  

  This is a real bug and I've had it happen to me in real-world code[*].

    cheers, 
      DaveK
[*] Well, a variation of it: no struct involved, just a const variable.
-- 
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]