Path: utzoo!attcan!uunet!lll-winken!lll-tis!ames!umd5!uflorida!novavax!proxftl!bill
From: bill@proxftl.UUCP (T. William Wells)
Newsgroups: comp.lang.c
Subject: Re: why do you need volatile?
Summary: summary
Message-ID: <333@proxftl.UUCP>
Date: 17 Jun 88 17:50:16 GMT
References: <15735@brl-adm.ARPA> <3754@pasteur.Berkeley.Edu>
Organization: Proximity Technology, Ft. Lauderdale
Lines: 28

In article <3754@pasteur.Berkeley.Edu>, faustus@ic.Berkeley.EDU (Wayne A. Christopher) writes:
) In article <15735@brl-adm.ARPA>, reg%lti.UUCP@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.