Can analysis detect undefined expressions?

Dave Harris Dave.Harris at f14.n15.z1.fidonet.org
Fri Jun 21 15:11:31 AEST 1991


In a message of <Jun 18 20:31>, Dale Worley (1:114/15) writes: 
> An extended example so that I can think clearly here:
>      (j = ((i=1) == (i=2))) == (j = ((i=3) == (i=4)))
> Assumedly, i can end up as 1,2,3, or 4.  j should be 0.  The grouping is 
such 
> that i=4, i=2, i=3, i=1 won't happen without breaking any laws.  right?

 >Sorry to belabor this yet again, but there is no requirement in Ansi C
 >that i have one of the values 1, 2, 3, or 4.  The effect of this
 >statement is "undefined", which means that the implementation can do
 >*anything*, including giving i the value 100, core dumping, or
 >starting World War III.  Ditto for j.  The mere fact that none of
 >these actions are mentioned in the statement is irrelevant.

Not arguing that the result is undefined as you say.  But....
I for one would quickly scrap any compiler that went to the additional work of 
embedding code to yield a value of anything other than 1,2,3 or 4 for i.  It 
would mean the compiler would have to detect the undefined statement first 
before it could even do this.  Its one thing for a compiler to give you a 
warning, but quite another for it to go out of its way to make mince meat out 
of your code.  It would be just one more thing that could go accidently wrong 
on perfectly good code.  The authors have a bad enough time with optimizer 
options ruining good code as it is.  Now, maybe there is a way of writing 
compilers such that undefined statemets yield completely unexpected results as 
a built in property of the algorithm.  The only thing that even remotely comes 
to mind might be some type of optimization that relies heavily on the 
assumption that nobody will write undefined expressions, but even that doesn't 
seem like a logical reason.

I would really like to see an example of code accomplishing completely 
unexpected behavior as in setting i equal to 5.  There probably isn't a 
compiler around that would do it because it would be extraneous work and risky 
to boot.

 


 

--  
Uucp: ...{gatech,ames,rutgers}!ncar!asuvax!stjhmc!15!14!Dave.Harris
Internet: Dave.Harris at f14.n15.z1.fidonet.org



More information about the Comp.lang.c mailing list