Re: volatile Info

crisgoogle <crisgoogle@xxxxxxxxx> writes:
On Aug 6, 4:26 am, Malcolm McLean <malcolm.mcle...@xxxxxxxxxxxxxx>
On Aug 6, 3:11 am, crisgoogle <crisgoo...@xxxxxxxxx> wrote:>
So I think Scott's question stands: how is the
answer different _without_ the "volatile"? If there's some bizarre mechanism
by which the value of, uh, "value" could change, then the returned result
may not be be the square of the original argument.

"volatile" is shorthand for "there is some bizarre mechanism by which
the value of this variable might change".

Okay. But is the _absence_ of volatile shorthand for "there is
NO bizarre mechanism by which the value of this variable might
change"? I don't think so.

I do. The absence of "volatile" means that the value of the
variable will change only by mechanisms defined by the C program
(unless the program's behavior is undefined for some reason, in
which case all bets are off).

So I ask yet again: without the "volatile", how does the answer
to the original question change?

The function in question was roughly (changing the name of the
parameter for clarity):

int square(volatile int x) {
return x * x;

Given that x is declared volatile, it's theoretically possible
that the two references to x in the return statement could yield
different values because some outside entity changed x's value
during the evaluation of the expression. Also, accessing x is by
definition a side effect (see; this side effect could
affect the visible behavior of the program. For example, accessing
x might implicitly cause it to be incremented.

When I say that something is "theoretically possible", I merely mean
that if it did happen, it would not violate the requirements of the
Standard, not that it's at all likely to happen under some actual
non-DS9K implementation.

Consider the following program:

#include <stdlib.h>

int square(volatile int x) {
return x * x;

int main(void) {
return square(10) == 100 ? EXIT_SUCCESS : EXIT_FAILURE;

If this program returns an unsuccessful termination status to
the environment, that doesn't imply that the implementation is

On the other hand, if a modified version of this program with the
"volatile" keyword deleted returns an unsuccessful termination status
to the environment, then the implementation *is* non-conforming.

"volatile" gives the implementation permission to cause a program
to behave in certain ways that it would not otherwise be permitted
to behave. An implementation is not obligated to take advantage
of this permission.

In this particular case, I'd be astonished if the "volatile"
keyword affected the visible behavior of the program (though it
might well affect the generated code). The authors of the standard,
quite rightly IMHO, did not restrict the use of "volatile" to cases
where it makes sense.

Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Relevant Pages

  • [git patches] net driver updates #2
    ... struct sk_buff *skb, int ring_offset) ... +static int use_mcs; ... volatile u8 RCR; ...
  • Re: Named shared memory without synchronization
    ... Since this is an int, my assumption was that this scenario would not benefit ... As you already figured out `volatile' specifier is required in order to tell the compiler that it should not assume any optimizations regarding a variable. ... Alignment is important because unaligned variable may require more than one instruction to access it, therefore breaching atimicity of an access. ... can ever be validated with testing when dealing with synchronization issues. ...
  • Re: matters of ordering, ...
    ... int * volatile x; ... However, if the incorrectly applied 'volatile' qualifier prevents your compiler from generating a mandatory diagnostic, or causes it to reject strictly conforming code, then your compiler is non-conforming. ... Something of that kind is necessary if you wish to support some C++ features while remaining fully conforming to C. ...
  • Re: Volatile happens before question
    ... before all subsequent reads. ... int b = 0; ... volatile boolean a = false; ... You may wish to invest in a copy of the book "Java Concurrency in Practice". ...
  • Re: ANSI C question about volatile
    ... > context related to 'volatile'. ... > volatile int x; ... I'm asking only what the standard ... On the other hand, if you want references to portions of the standard, ...