Path: utzoo!utgpu!water!watmath!clyde!bellcore!faline!thumper!ulysses!ucbvax!pasteur!ic.Berkeley.EDU!faustus From: faustus@ic.Berkeley.EDU (Wayne A. Christopher) Newsgroups: comp.lang.c Subject: Re: why do you need volatile? Message-ID: <3754@pasteur.Berkeley.Edu> Date: 2 Jun 88 22:44:05 GMT References: <15735@brl-adm.ARPA> Sender: news@pasteur.Berkeley.Edu Lines: 24 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. These are limited to (I think) signal handlers and device registers. If your program doesn't use either of these, you don't have to declare anything volatile. Wayne