[PATCH] C undefined behavior fix

Matthias Benkmann matthias@winterdrache.de
Thu Jan 10 03:03:00 GMT 2002


On 9 Jan 2002, at 19:19, Peter Barada wrote:

> 
> >Even
> >
> >int	b;
> >volatile const int a = 5;
> >b = a - a;
> >
> >can not be optimized to 
> >
> >b = 0;
> 
> Until you define the scope of the variables, you can't make that
> assertion.  If the code is:
> 
> int b;
> volatile const a=5;
> void stuff()
> {
>   b = a - a;
> }
> 
> I can see how a can change in the midst of the execution since
> some other code has access to a since its global scope.
> 
> If the code is:
> 
> int b;
> void stuff()
> {
>   volatile const a=5;
> 
>   b = a - a;
> }
> 
> Then the code can be optimized to 'b = 0;' since nowhere in the scope
> of 'a' does anyone take its address(which would allow it to be changed).

I could have hardware attached directly to the data bus (a hardware 
debugger for instance) that watches for the value 5 to appear. This is 
beyond the knowledge of the compiler.

> 2) No one takes the address of b, so there is no way for any external
>    hardware/thread to modify b.

see above. You don't need to take the address to catch the variable 
access.
 
MSB

-- 
Who is this General Failure,
and why is he reading my disk ?



More information about the Gcc mailing list