Re: const + optimizations [WAS: Need C optimization help]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Enrico Weigelt <weigelt@xxxxxxxx> writes:

> * Segher Boessenkool <segher@xxxxxxxxxxxxxxxxxxx> wrote:
>  
>> Modifying an object via a const lvalue is a constraint violation; any
>> program that does this is not a valid C program.
>
> Is the compiler able to use that for optimizations (eg. hold
> const variables in registers, etc) ?

It depends.  Modifying an object via a const lvalue is invalid, but it
is possible for the object to be modified in other ways.  E.g., a const
char* pointer may point to a value which is modified via a normal char*
pointer.  An object which is itself declared const may not be modified
in any way, and the compiler does take advantage of that.

>> Modifying a const object via a non-const lvalue is undefined behaviour.
>
> Is there a flag to forbid that ?

-Wcast-qual -Werror.

Ian


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux