Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Volatile

Reply
Thread Tools

Volatile

 
 
Christian Christmann
Guest
Posts: n/a
 
      07-31-2006
Hi,

the keyword 'volatile' indicates that a variable
may be modified externally. The occurance of this
keyword in a function often symbolizes a compiler
not to optimize this entire function.

Why is it a problem for a compiler to analyze and improve
such functions? Why does the fact that the variable
may be changed at any point of the program execution avoids
any code reorganization (for the "volatile-specific" code)?

Regards,
Chris
 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      07-31-2006
Christian Christmann said:

> Hi,
>
> the keyword 'volatile' indicates that a variable
> may be modified externally. The occurance of this
> keyword in a function often symbolizes a compiler
> not to optimize this entire function.
>
> Why is it a problem for a compiler to analyze and improve
> such functions? Why does the fact that the variable
> may be changed at any point of the program execution avoids
> any code reorganization (for the "volatile-specific" code)?


Consider the following code:

int func(void)
{
int *p = getstartaddress(FOO);

while(*p != *p)
{
hang_on_for(5);
}

do_something();
}

The compiler can legitimately replace this code with:

int func(void)
{
getstartaddress(FOO);
do_something();
}

But if we change it from:

int *p = getstartaddress(FOO);

to

volatile int *p = getstartaddress(FOO);

the compiler is no longer allowed to optimise away the object, the loop
test, and the function call within the loop. The compiler is required to
fetch *p twice in the loop test, and it is possible for *p to change in the
interim. If it does, hang_on_for(5) will be called.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      07-31-2006
Christian Christmann wrote:

> Hi,
>
> the keyword 'volatile' indicates that a variable
> may be modified externally. The occurance of this
> keyword in a function often symbolizes a compiler
> not to optimize this entire function.
>
> Why is it a problem for a compiler to analyze and improve
> such functions? Why does the fact that the variable
> may be changed at any point of the program execution avoids
> any code reorganization (for the "volatile-specific" code)?


Consider an I/O device that receives commands from the
program by monitoring what is written to a particular memory
address ("memory-mapped I/O registers"). We might try to
send a series of commands to such a device like this:

unsigned int *ioreg = ...; /* point to special address */
*ioreg = IO_RESET; /* reset device */
*ioreg = IO_MODE_BCAST; /* select broadcast mode */
*ioreg = IO_START; /* start operation */

(These "commands" are for illustrative purposes only; they're
not to be taken too seriously. The important point is that it
takes a sequence of several commands to get the device into
the desired state.)

Without `volatile', the compiler might notice that the first
two assignments have to effect: They simply store a value that
the program never uses and promptly overwrites, so they are not
necessary, right? The compiler might well just eliminate the
"useless" assignments and act as if you had written

unsigned int *ioreg = ...; /* point to special address */
*ioreg = IO_START; /* start operation (?)*/

By writing `volatile unsigned int *ioreg = ...;' we notify the
compiler that this is not permissible: The "useless" assignments
are in fact necessary and must not be "optimized" away.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ERROR [HY000] [Microsoft][ODBC Microsoft Access Driver]General error Unable to open registry key 'Temporary (volatile) Jet DSN for process 0xffc Thread 0x228 DBC 0x437b94 Jet'. ERROR [IM006] [Microsoft][ODBC Driver Manager] Driver's SQLSetConnectAttr bazzer ASP .Net 0 03-30-2006 03:16 PM
ERROR [HY000] [Microsoft][ODBC Microsoft Access Driver]General error Unable to open registry key 'Temporary (volatile) Jet DSN for process 0x8fc Thread 0x934 DBC 0x437b94 Jet'. ERROR [IM006] [Microsoft][ODBC Driver Manager] Driver's SQLSetConnectAttr bazzer ASP .Net 1 03-24-2006 04:20 PM
ERROR [HY000] [Microsoft][ODBC Microsoft Access Driver]General error Unable to open registry key 'Temporary (volatile) Jet DSN for process 0x8fc Thread 0x934 DBC 0x437b94 Jet'. ERROR [IM006] [Microsoft][ODBC Driver Manager] Driver's SQLSetConnectAttr bazzer ASP .Net 0 03-24-2006 02:22 PM
Use of the Volatile keyword for a pointer to a volatile memory block ben C Programming 5 01-11-2005 05:38 PM
Re: Volatile? Knute Johnson Java 17 07-03-2003 03:31 AM



Advertisments