Volatile

Discussion in 'C Programming' started by Christian Christmann, Jul 31, 2006.

  1. 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
    Christian Christmann, Jul 31, 2006
    #1
    1. Advertising

  2. 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)
    Richard Heathfield, Jul 31, 2006
    #2
    1. Advertising

  3. Christian Christmann

    Eric Sosman Guest

    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
    lid
    Eric Sosman, Jul 31, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Knute Johnson

    Re: Volatile?

    Knute Johnson, Jul 1, 2003, in forum: Java
    Replies:
    17
    Views:
    1,450
    Knute Johnson
    Jul 3, 2003
  2. Doug Pardee

    Re: Volatile?

    Doug Pardee, Jul 1, 2003, in forum: Java
    Replies:
    0
    Views:
    897
    Doug Pardee
    Jul 1, 2003
  3. Replies:
    1
    Views:
    915
    Knute Johnson
    Aug 29, 2003
  4. Daniel

    Can volatile be trusted?

    Daniel, Sep 11, 2003, in forum: Java
    Replies:
    7
    Views:
    473
    Chris Uppal
    Sep 12, 2003
  5. ben
    Replies:
    5
    Views:
    594
    Ulrich Eckhardt
    Jan 11, 2005
Loading...

Share This Page