why do you need volatile?

Wayne A. Christopher faustus at ic.Berkeley.EDU
Fri Jun 3 08:44:05 AEST 1988


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.  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



More information about the Comp.lang.c mailing list