Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Message

Re: [AVR-Chat] Re: Volatile modifier

2012-04-20 by R E Purcella

I think you are nit picking words.

Optimized value was explained by Gregory N.

Many C, C++ code lines are directives to the compiler: include, define . 
. . Apparently volatile is as well.

I suppose that volatile is indeed far more complex in execution. But, 
hardly needed in order to type in "volatile int speed;" So long as I do 
understand that speed might be changed in unexpected ways by the 
compiler when it optimizes my code. I therefore protect my code, and 
variable, by saying VOLATILE. Use volatile variables in ISR routines or 
strange things happen.

I see it then as an optimization switch. Namely, optimization gets 
turned off for the code where a volatile variable occurs.

And that has been the gobblegook point of several writers all along.

rep



On 4/20/2012 7:04 AM, bayramdavies wrote:
>
>
>
> R E Purcella wrote:
>
> > ... "the complete reference C++" by Herbert
> > Schildt, third edition ... [says] ... "The
> > modifier volatile tells the compiler that a
> > variable's value may be changed in ways not
> > explicitly specified by the program."
>
> You see, this isn't actually correct. I think the author is trying to 
> make a compromise between telling us what the volatile type qualifier 
> (not "modifier", there's the first mistake) actually is and when to 
> use it. Sentences involving what you "tell the compiler" are always 
> going to be tricky. If you're "telling" the compiler anything with the 
> volatile qualifier, you're telling it that accesses to the qualified 
> object (not variable, second mistake) must be done in strict 
> conformity with the abstract machine used to define the semantics of 
> the C language in the specification. You're not telling the compiler 
> why you're asking for this (third mistake) and the "value may be 
> changed in ways not explicitly specified by the program" is only one 
> of the reasons you might want strict conformance (fourth mistake).
>
> > My friend says "... you need to declare that
> > variable as volatile, to tell the compiler
> > to not use the optimized value ..."
>
> What the heck is "the optimized value"? This is nonsense. If you think 
> you understand it, then that's because you think you already 
> understand volatile and you're able to map this gobbledygook onto your 
> understanding.
>
> > So I understand him to be saying Volatile
> > tells the compiler 1) not to use an optimized
> > value 2) not to assume that the value hasn't
> > changed
>
> This is a poor description of volatile for reasons presented above.
>
> I have yet to come across a description of volatile that conforms to 
> what is written in the standard, is complete, correct and is in all 
> situations helpful to people who do not fully understand the 
> terminology of the standard, compilation and software engineering in 
> general. I am starting to think that nobody would read such a 
> description as it would necessarily be quite long and people's 
> preference is for short over complete and correct.
>
> You might think, for example, that Keil (part of ARM) could get this 
> right. But, they gave the job of writing this description of the 
> volatile type qualifier:
>
> http://www.keil.com/support/man/docs/c51/c51_le_volatile.htm
>
> to someone who has a pretty good understanding of the C language but 
> no understanding of hardware. Can you spot the glaring problem?
>
> Graham Davies
> ECROS Technology
> www.ecrostech.com
>
>

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.