From: Charles Krug Newsgroups: comp.os.msdos.djgpp Subject: Re: A modifier -- volatile Date: Mon, 26 Jan 1998 10:01:28 -0500 Lines: 34 Message-ID: <34CCA548.C78F2911@pentek.com> References: <199801252242 DOT RAA29221 AT delorie DOT com> NNTP-Posting-Host: mail.pentek.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk VAMPYR wrote: > A syntax: > > volatile int variable; > > But what is the "volatile" means? Volatile is a variable whose contents are subject to change outside the control of the program. If you have a memory mapped device, that device will change its registers according to its status, hence the memory associated with it is "volatile." The program SHOULD re-load the memory contents every time its used (some compilers forget, which is a serious fault) This usually bites you on the butt when you're code is "finished" and you try to run with full optimization. The compiler sees you accessing your device register in a loop. Consequently, it loads the location into a register as an optimization. Unfortunatly, the register no longer reflects the status of the device, causing the code to fail--usually in interesting ways. Then you disable optimizations so you can use gdb, whereupon the code works, and you pull your hair out for three weeks until someone says casually, "That's an external register. Shouldn't it be 'volatile'". -- Charles Krug, Jr. Application Engineer Pentek Corp 1 Park Way Upper Saddle River, NJ 07458