why do you need volatile?

T. William Wells bill at proxftl.UUCP
Sat Jun 18 03:50:16 AEST 1988


In article <3754 at pasteur.Berkeley.Edu>, faustus at ic.Berkeley.EDU (Wayne A. Christopher) writes:
) In article <15735 at brl-adm.ARPA>, reg%lti.UUCP at bu-it.bu.edu (Rick Genter x18) writes:
) >     for (A = 1.0, i = 0 ; i < iCalcRep ; i++) {
) >             if (! bContinueCalc)
) >                     break;
) >             A = Savage (A);
) >     }
) > ... should be declared
) >
) >     volatile BOOL bContinueCalc = FALSE;
) >
) > otherwise the C compiler is perfectly justified taking the test of
) > bContinueCalc out of the for loop, thus invalidating the whole use
) > of the variable.
)
) I don't think this is right.  If bContinueCalc were an automatic variable
) that never had its address taken, or there were no function call inside the
) loop, then the test could be taken out, but not otherwise.  "volatile"
) doesn't mean that more than one function can modify a variable, but that
) a variable can become modified by an event that happens wholly outside of
) the control of the program.

Not quite, a nonvolatile variable is presumed to be modified only
by execution in the standard C paradigm.  This implies single
thread execution, among other things.  Now, if bContinueCalc is
demonstrably not changed in Savage(), the compiler is justified
in moving the test outside the loop.  To prevent that, one should
declare it volatile, meaning that something outside the normal
flow of execution can change it.



More information about the Comp.lang.c mailing list