[Top][All Lists]

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

Re: [avr-gcc-list] "Volatile"

From: Graham Davies
Subject: Re: [avr-gcc-list] "Volatile"
Date: Fri, 15 Apr 2005 21:47:44 -0400

Keith Gudger wrote:

> ... I'm wondering if the code that "got
> me" is as obvious as the other volatile stuff.


> 1)  Is this something I should have learned
> in Embedded 101?

Since nobody has answered this question, only the other one about how the
experts deal with it, I would like to say that in my opinion you should not
be too hard on yourself for missing this.  It is true that your
misunderstanding of the effect of volatile broke the system, but in embedded
systems pretty much every failure of understanding breaks the system in some
way ... this is why it is so difficult.

I have had to work with software engineers who have worked in embedded
systems for several years and cannot tell you the effect of the static
modifier, let alone volatile.  A popular misconception was that these
keyword affect whether or not a variable is cached (pause for laughter).

Even on this list, some of the experts are not 100% clued in.  A while ago I
asked if I was wrong to expect that an assignment to a volatile object
should result in exactly one machine-level write to the memory location in
which it is stored.  Several responders told me that I should not expect
this and that the compiler is at liberty to implement the assignment as
multiple writes even when the variable is declared volatile.  Further
research indicated that this was wrong and my expectation is correct.

People often only come up with half of the rationale for using volatile,
which is to state that the value of the variable can be altered in ways that
the compiler cannot detect.  The other reason to use volatile is to state
that access to the variable may have side effects.  In both cases, the
result, as has been stated in this thread, is to prohibit the compiler from
making certain optimizations and to constrain it to follow *exactly* the
behavior of the "virtual machine" which is at the core of the CO language

I hope this is helpful.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]