Assignment to volatile objects

David Brown david@westcontrol.com
Tue Jan 31 13:15:00 GMT 2012


On 30/01/2012 23:59, Zoltán Kócsi wrote:
> David Brown<david.brown@hesbynett.no>  wrote:
>
>> Until gcc gets a feature allowing it to whack the programmer on the back
>> of the head with Knuth's "The Art of Computer Programming" for writing
>> such stupid code that relies on the behaviour of volatile "a = b = 0;",
>> then a warning seems like a good idea.
>
> a = b = 0; might be stupid.
>

For normal variables, "a = b = 0" is just ugly - but that is a matter of 
opinion.

But if one of "a" or "b" is volatile, it is stupid.

When you are using "volatile", you are telling the compiler "go slow, 
tread carefully, and do exactly as I tell you".  It should also tell the 
programmer "go slow, tread carefully, and write exactly what you mean".

So if you are using volatile variables, you don't write code that is 
open to questionable interpretation or misinterpretation - either by the 
standards committees, the compiler authors, the programmer, or by people 
reading the code.

> Is if ( ( a = expr ) ); is also stupid?
>

Again, for normal variables it is just ugly (/very/ occasionally it 
might look clear and be appropriate style).  If any of it involves 
volatiles, it is stupid.

> I thought that that idiom was cited as an example for the expressiveness of C
> in the C bible (the K&R book).
>
> Zoltan
>

K&R is the C "Bible" in the same way that Genesis and Exodus are the 
Christian Bible.  It is only part of the story - it is interesting 
history, and some interesting viewpoints, but a lot of it is badly 
outdated, and much of it was terrible in the first place.  Never forget 
that a guiding factor in the design of C was that K & R didn't like the 
keyboards they had to use - saving keystrokes was more important to them 
than good style or clear and reliable programming.  And never forget 
that K&R expected their readers to /think/, not follow blindly.  An 
"example of the expressiveness of C" is just that - an example of the 
/expressiveness/ of C - and not an example of good C programming style.

When you are writing C code, /you/ are the one that determines what is 
good style - not a forty year old book.  If /you/ have to ask questions 
about the interpretation of some code, then the code is wrong.

mvh.,

David



More information about the Gcc mailing list