Volatile

  • Thread starter Christian Christmann
  • Start date
C

Christian Christmann

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
 
R

Richard Heathfield

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

Eric Sosman

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top