This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Does the keyword volatile work?
On 12-May-2003, Stephen P. Smith <ischis2@cox.net> wrote:
> Fergus Henderson wrote:
>
> >On 12-May-2003, Stephen P. Smith <ischis2@cox.net> wrote:
> >
> >Firstly, I would suggest using `*(volatile DWORD *)&p1'
> >rather than `(volatile DWORD)p1'.
>
> Why would this be better syntax?
With this syntax, the value being accessed with volatile type is
`*(&p1)', i.e. `p1'.
With your original syntax, the value being accessed with volatile
type is not `p', but rather the expression `(volatile DWORD)p1'.
This expression is an rvalue, not an lvalue or an object.
The wording in the standard prohibits optimizing away accesses to volatile
*objects*, but doesn't say anything about volatile rvalues.
> >Secondly, if you want to be sure that the value will be assigned to
> >the target variable immediately, you need to declare the target
> >variable as volatile.
>
> Why is that the case? I thought that the cast would be sufficient.
Marking the source variable of the assignment as volatile tells the
compiler to not optimize away the loading of the value of the source
variable (at least if you do this marking properly -- see above),
but doesn't affect the storing of the result, since the target of the
store is not volatile.
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
The University of Melbourne | of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh> | -- the last words of T. S. Garp.