Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!zaphod.mps.ohio-state.edu!mips!apple!oliveb!olivee!swirsky From: swirsky@olivee.olivetti.com (Robert Swirsky) Newsgroups: comp.unix.wizards Subject: Re: Is your system polluted? Summary: Another Good Reason for volatile keyword Keywords: volatile Message-ID: <52900@oliveb.olivetti.com> Date: 26 Dec 89 16:59:57 GMT References: <1552@aber-cs.UUCP> <259323F0.15070@paris.ics.uci.edu> <42449@lll-winken.LLNL.GOV> Sender: news@oliveb.olivetti.com Reply-To: swirsky@olivee.UUCP (Robert Swirsky) Organization: Olivetti ATC; Cupertino, Ca Lines: 25 There's another place where the volatile keyword is useful--if there's a variable whose value is set in an interrupt service routine. Many Cs (e.g., Microsoft's C for Unix) have an interrupt keyword so that a function saves all its registers and ends with an IRET. If the compiler does invarient code optimization, it can incorrectly see code as invarient without the "volatile" keyword. For example: do { /* blah blah blah */ } while (flag==0); If "flag" is set in an interrupt service routine, some compilers would move the test for flag being zero out of the loop, because no statement within the loop changes its value. The result is a loop that never exits. I've seen this happen with MSC for DOS... "All opinions are my own, and may not be those of my employer."