volatile

Discussion in 'C Programming' started by Ravi, Oct 12, 2003.

  1. Ravi

    Ravi Guest

    I have read about this qualifier in many books and have read
    things like "would not let the compiler optimize" "can be
    modified externally" etc. But I am not able to understand it
    well.

    Could someone give me a small example and explain the
    meaning and use of the volatile qualifier.

    TIA.

    --
    main(){char s[40]="sbwjAeftqbnnfe/dpn!ps!CSbwjACjhgppu/dpn";
    int i;for(i=0;i<39;putchar(s[i++]-1));return 0;}
     
    Ravi, Oct 12, 2003
    #1
    1. Advertising

  2. On Sun, 12 Oct 2003 22:46:05 +0530, Ravi wrote:

    > I have read about this qualifier in many books and have read
    > things like "would not let the compiler optimize" "can be
    > modified externally" etc. But I am not able to understand it
    > well.
    >
    > Could someone give me a small example and explain the
    > meaning and use of the volatile qualifier.


    Consider the program

    int x;
    int main (void)
    {
    int i;
    for (i = 0; i < 4; ++i)
    x = i;
    return x;
    }

    When a compiler optimizes this program, it is free to change it
    to something like

    int main (void)
    {
    return 3;
    }

    because this program has the same behavior as the original one.
    When the original program is changed to

    volatile int x;
    int main (void)
    {
    int i;
    for (i = 0; i < 4; ++i)
    x = i;
    return x;
    }

    then the assignments to x must take place. They may not be
    optimized away. The best a compiler could do to optimize
    the code now is something like:

    volatile int x;
    int main (void)
    {
    x = 0;
    x = 1;
    x = 2;
    x = 3;
    return 3;
    }

    Footnote 114 in the C standard explains it like this:

    A volatile declaration may be used to describe an object
    corresponding to a memory-mapped input/output port or an
    object accessed by an asynchronously interrupting function.
    Actions on objects so declared shall not be optimized out
    by an implementation or reordered except as permitted by
    the rules for evaluating expressions.

    -Sheldon
     
    Sheldon Simms, Oct 12, 2003
    #2
    1. Advertising

  3. On Sun, 12 Oct 2003 14:35:01 -0400, Sheldon Simms wrote:

    > On Sun, 12 Oct 2003 22:46:05 +0530, Ravi wrote:
    >
    >> I have read about this qualifier in many books and have read
    >> things like "would not let the compiler optimize" "can be
    >> modified externally" etc. But I am not able to understand it
    >> well.
    >>
    >> Could someone give me a small example and explain the
    >> meaning and use of the volatile qualifier.

    >
    > Consider the program
    >
    > int x;
    > int main (void)
    > {
    > int i;
    > for (i = 0; i < 4; ++i)
    > x = i;
    > return x;
    > }
    >
    > When a compiler optimizes this program, it is free to change it
    > to something like
    >
    > int main (void)
    > {
    > return 3;
    > }
    >
    > because this program has the same behavior as the original one.
    > When the original program is changed to
    >
    > volatile int x;
    > int main (void)
    > {
    > int i;
    > for (i = 0; i < 4; ++i)
    > x = i;
    > return x;
    > }
    >
    > then the assignments to x must take place. They may not be
    > optimized away. The best a compiler could do to optimize
    > the code now is something like:
    >
    > volatile int x;
    > int main (void)
    > {
    > x = 0;
    > x = 1;
    > x = 2;
    > x = 3;
    > return 3;
    > }


    Oops-- this should be

    volatile int x;
    int main (void)
    {
    x = 0;
    x = 1;
    x = 2;
    x = 3;
    return x;
    }

    >
    > Footnote 114 in the C standard explains it like this:
    >
    > A volatile declaration may be used to describe an object
    > corresponding to a memory-mapped input/output port or an
    > object accessed by an asynchronously interrupting function.
    > Actions on objects so declared shall not be optimized out
    > by an implementation or reordered except as permitted by
    > the rules for evaluating expressions.
    >
    > -Sheldon
     
    Sheldon Simms, Oct 12, 2003
    #3
  4. Ravi

    Ravi Guest

    On Sun, 12 Oct 2003 14:35:01 -0400, Sheldon Simms
    <> wrote:

    >Footnote 114 in the C standard explains it like this:
    >
    > A volatile declaration may be used to describe an object
    > corresponding to a memory-mapped input/output port or an
    > object accessed by an asynchronously interrupting function.
    > Actions on objects so declared shall not be optimized out
    > by an implementation or reordered except as permitted by
    > the rules for evaluating expressions.

    I've read these things.


    I understand this. But I want a proper example clearly
    telling me the use of declaring a variable volatile etc.

    TIA.

    --
    main(){char s[40]="sbwjAeftqbnnfe/dpn!ps!CSbwjACjhgppu/dpn";
    int i;for(i=0;i<39;putchar(s[i++]-1));return 0;}
     
    Ravi, Oct 12, 2003
    #4
  5. Ravi <> scribbled the following:
    > On Sun, 12 Oct 2003 14:35:01 -0400, Sheldon Simms
    > <> wrote:


    >>Footnote 114 in the C standard explains it like this:
    >>
    >> A volatile declaration may be used to describe an object
    >> corresponding to a memory-mapped input/output port or an
    >> object accessed by an asynchronously interrupting function.
    >> Actions on objects so declared shall not be optimized out
    >> by an implementation or reordered except as permitted by
    >> the rules for evaluating expressions.

    > I've read these things.


    > I understand this. But I want a proper example clearly
    > telling me the use of declaring a variable volatile etc.


    > TIA.


    Didn't Sheldon just give you one?

    Another one might be that some pointer variable holds a memory address
    where an external hardware device keeps reading stuff from. The C
    program periodically assigns new values to this address.
    Because the C program never actually *reads* from this address, an
    optimising compiler might optimise the entire assignments away, leaving
    the external hardware device with nothing to read. Therefore the
    pointer variable needs to be declared volatile, telling the compiler
    to keep the assignments in even though they appear to serve no
    purpose.

    --
    /-- Joona Palaste () ---------------------------\
    | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
    | http://www.helsinki.fi/~palaste W++ B OP+ |
    \----------------------------------------- Finland rules! ------------/
    "War! Huh! Good God, y'all! What is it good for? We asked Mayor Quimby."
    - Kent Brockman
     
    Joona I Palaste, Oct 12, 2003
    #5
  6. Ravi

    Michael Str. Guest

    Sheldon Simms <> wrote in message news:<>...
    > On Sun, 12 Oct 2003 14:35:01 -0400, Sheldon Simms wrote:
    >
    > > On Sun, 12 Oct 2003 22:46:05 +0530, Ravi wrote:
    > >
    > >> I have read about this qualifier in many books and have read
    > >> things like "would not let the compiler optimize" "can be
    > >> modified externally" etc. But I am not able to understand it
    > >> well.
    > >>
    > >> Could someone give me a small example and explain the
    > >> meaning and use of the volatile qualifier.

    > >
    > > Consider the program
    > >
    > > int x;
    > > int main (void)
    > > {
    > > int i;
    > > for (i = 0; i < 4; ++i)
    > > x = i;
    > > return x;
    > > }
    > >
    > > When a compiler optimizes this program, it is free to change it
    > > to something like
    > >
    > > int main (void)
    > > {
    > > return 3;
    > > }
    > >
    > > because this program has the same behavior as the original one.
    > > When the original program is changed to
    > >
    > > volatile int x;
    > > int main (void)
    > > {
    > > int i;
    > > for (i = 0; i < 4; ++i)
    > > x = i;
    > > return x;
    > > }
    > >
    > > then the assignments to x must take place. They may not be
    > > optimized away. The best a compiler could do to optimize
    > > the code now is something like:
    > >
    > > volatile int x;
    > > int main (void)
    > > {
    > > x = 0;
    > > x = 1;
    > > x = 2;
    > > x = 3;
    > > return 3;
    > > }

    >
    > Oops-- this should be
    >
    > volatile int x;
    > int main (void)
    > {
    > x = 0;
    > x = 1;
    > x = 2;
    > x = 3;
    > return x;
    > }
    >
    > >
    > > Footnote 114 in the C standard explains it like this:
    > >
    > > A volatile declaration may be used to describe an object
    > > corresponding to a memory-mapped input/output port or an
    > > object accessed by an asynchronously interrupting function.
    > > Actions on objects so declared shall not be optimized out
    > > by an implementation or reordered except as permitted by
    > > the rules for evaluating expressions.
    > >
    > > -Sheldon


    I think good description of volatile can be found in:
    http://www.embedded.com/story/OEG20010615S0107

    Regards
    Michael
     
    Michael Str., Oct 12, 2003
    #6
  7. Ravi <> wrote in
    news: on Sun 12 Oct 2003
    01:15:44p:


    > I understand this. But I want a proper example clearly
    > telling me the use of declaring a variable volatile etc.


    What, exactly, is unclear? To make a variable volatile, simply use the
    keyword 'volatile'. Then use it as you normally would use a variable of
    that type. There's no special tricks, just a hint to the compiler that it
    should not perform certain optimizations (if, indeed, it optimizes at
    all).

    Other posts in the thread tell you what that can actually mean in your
    code, and I hope you realize that overuse of the qualifier can lead to
    code that the compiler basically cannot improve because so many
    optimizations are no longer available. Of course, the qualifier can also
    make things possible that the compiler would otherwise optimize away.
     
    August Derleth, Oct 12, 2003
    #7
  8. On Mon, 13 Oct 2003 00:33:44 +0530, Ravi <> wrote:

    >On Sun, 12 Oct 2003 14:35:01 -0400, Sheldon Simms
    ><> wrote:
    >
    >>Footnote 114 in the C standard explains it like this:
    >>
    >> A volatile declaration may be used to describe an object
    >> corresponding to a memory-mapped input/output port or an
    >> object accessed by an asynchronously interrupting function.
    >> Actions on objects so declared shall not be optimized out
    >> by an implementation or reordered except as permitted by
    >> the rules for evaluating expressions.

    >I've read these things.
    >
    >
    >I understand this. But I want a proper example clearly
    >telling me the use of declaring a variable volatile etc.


    Any proper example would probably be implementation dependent. You
    haven't told us what system you are using or which other ones you are
    familiar enough with to understand the example if presented.

    And if you did it would be off-topic here so you are better off asking
    in a group devoted to your system or one you understand.


    <<Remove the del for email>>
     
    Barry Schwarz, Oct 12, 2003
    #8
  9. Ravi

    osmium Guest

    August Derleth writes:

    > There's no special tricks, just a hint to the compiler that it
    > should not perform certain optimizations (if, indeed, it optimizes at
    > all).


    It is not a hint! It is a directive.
     
    osmium, Oct 13, 2003
    #9
  10. Ravi

    Ramaraj Guest

    Hi All,
    The volatile Keyword is frequently used in the application like drivres
    for the memory mapped device etc. if the execution control is one
    function/block and some external events changes the values of volatile
    variable without interrupting the current execution functional/block. so
    that optimization of the compiler is resticted here bcoz the voalitile
    variable is runtime entity it
    can be changes(regardless functional execution control) .

    I can give the example as follows

    we can bind the volatile variable "i" to clock tick and the variable changes
    during the without knowing the current execution functional or block


    "Ravi" <> wrote in message
    news:...
    > I have read about this qualifier in many books and have read
    > things like "would not let the compiler optimize" "can be
    > modified externally" etc. But I am not able to understand it
    > well.
    >
    > Could someone give me a small example and explain the
    > meaning and use of the volatile qualifier.
    >
    > TIA.
    >
    > --
    > main(){char s[40]="sbwjAeftqbnnfe/dpn!ps!CSbwjACjhgppu/dpn";
    > int i;for(i=0;i<39;putchar(s[i++]-1));return 0;}
     
    Ramaraj, Oct 13, 2003
    #10
  11. Ravi

    Ravi Guest

    On 12 Oct 2003 22:43:20 GMT, Barry Schwarz
    <> wrote:

    >On Mon, 13 Oct 2003 00:33:44 +0530, Ravi <> wrote:
    >
    >>On Sun, 12 Oct 2003 14:35:01 -0400, Sheldon Simms
    >><> wrote:
    >>
    >>>Footnote 114 in the C standard explains it like this:
    >>>
    >>> A volatile declaration may be used to describe an object
    >>> corresponding to a memory-mapped input/output port or an
    >>> object accessed by an asynchronously interrupting function.
    >>> Actions on objects so declared shall not be optimized out
    >>> by an implementation or reordered except as permitted by
    >>> the rules for evaluating expressions.

    >>I've read these things.
    >>
    >>
    >>I understand this. But I want a proper example clearly
    >>telling me the use of declaring a variable volatile etc.

    >
    >Any proper example would probably be implementation dependent. You
    >haven't told us what system you are using or which other ones you are
    >familiar enough with to understand the example if presented.


    Linux, Windows or DOS.

    TIA.

    --
    main(){char s[40]="sbwjAeftqbnnfe/dpn!ps!CSbwjACjhgppu/dpn";
    int i;for(i=0;i<39;putchar(s[i++]-1));return 0;}
     
    Ravi, Oct 13, 2003
    #11
  12. Ravi

    Kevin Bracey Guest

    In message <bmcle8$va0$0@216.39.134.220>
    Barry Schwarz <> wrote:

    > On Mon, 13 Oct 2003 00:33:44 +0530, Ravi <> wrote:
    >
    > >I understand this. But I want a proper example clearly
    > >telling me the use of declaring a variable volatile etc.

    >
    > Any proper example would probably be implementation dependent. You
    > haven't told us what system you are using or which other ones you are
    > familiar enough with to understand the example if presented.
    >
    > And if you did it would be off-topic here so you are better off asking
    > in a group devoted to your system or one you understand.


    Blimey, we're all being unhelpful today, aren't we? A whole keyword pretty
    much declared off-topic...

    volatile solves a real-world problem in low-level programming. It exists to
    stop clever compilers from over-optimising code. You won't see it discussed
    here very much for that very reason - comp.lang.c doesn't generally do
    real-world problems, and doesn't concern itself with things like
    optimisation.

    Here's a pretty realistic example:

    volatile char *serial_port;

    /* Wait until we receive '>' over the serial port,
    * then send "Hello".
    */
    void say_hello(void)
    {
    while (*serial_port != '>')
    ;

    *serial_port = 'H';
    *serial_port = 'e';
    *serial_port = 'l';
    *serial_port = 'l';
    *serial_port = 'o';
    }

    The idea here is that "serial_port" is pointing at a memory-mapped read- and
    write-sensitive I/O port. Without the volatile qualifier, the compiler would
    be free to assume that repeatedly reading from that address would always
    return the same value, and that multiple writes were irrelevant. It might end
    up optimising it to something like:

    void say_hello(void)
    {
    char temp = *serial_port
    while (temp != '>')
    ;

    *serial_port = 'o';
    }

    which was clearly not the intent.

    Another possible use of volatile is when you might be sharing a variable
    with another thread or an interrupt routine, so it is important that the
    variable's content be kept up-to-date and should be rechecked every time it
    is used.

    --
    Kevin Bracey, Principal Software Engineer
    Tematic Ltd Tel: +44 (0) 1223 503464
    182-190 Newmarket Road Fax: +44 (0) 1223 503458
    Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/
     
    Kevin Bracey, Oct 13, 2003
    #12
  13. Ravi

    Dan Pop Guest

    In <> Ravi <> writes:

    >I have read about this qualifier in many books and have read
    >things like "would not let the compiler optimize" "can be
    >modified externally" etc. But I am not able to understand it
    >well.
    >
    >Could someone give me a small example and explain the
    >meaning and use of the volatile qualifier.


    Consider the following example:

    #include <stdio.h>
    #include <signal.h>

    sig_atomic_t gotsig = 0;

    void handler(int signo)
    {
    gotsig = signo;
    }

    int main()
    {
    signal(SIGINT, handler);
    puts("Press the interrupt key to exit.");
    while (gotsig == 0) ;
    printf ("The program received signal %d.\n", (int)gotsig);
    return 0;
    }

    If you compile it with gcc without enabling optimisation, it will work
    as expected. With -O2, it will stay forever in the while loop, because
    the compiler assumes that the value of gotsig cannot change inside the
    loop, so there is no need to check its value more than once: the loop is
    either skipped or it becomes an infinite loop.

    To avoid this, you must tell to the compiler that it cannot assume that
    the value of gotsig cannot change inside the loop (otherwise, a perfectly
    reasonable assumption). To achieve this, you declare gotsig like this:

    volatile sig_atomic_t gotsig = 0;

    Now, the program will work as expected at any optimisation level (at least
    as far as gcc is concerned ;-)

    Another typical example is a program performing memory tests. Such
    programs write certain values to the memory, then read them back and
    check to see if they get the same values. From the compiler's point of
    view, such an operation is completely pointless: if you write something
    you're guaranteed to read back the same value, so it could optimise the
    whole code away. To avoid that, you have to either make the pointer
    volatile or the object it points to or both, but it's probably more
    efficient to make only the object pointed to volatile.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Oct 13, 2003
    #13
  14. On Mon, 13 Oct 2003 13:53:58 +0530, Ravi <> wrote:

    >On 12 Oct 2003 22:43:20 GMT, Barry Schwarz
    ><> wrote:
    >

    snip
    >>Any proper example would probably be implementation dependent. You
    >>haven't told us what system you are using or which other ones you are
    >>familiar enough with to understand the example if presented.

    >
    >Linux, Windows or DOS.
    >
    >TIA.


    Your not welcome. What part of the next paragraph which you
    conveniently cut out (which said "And if you did it would be off-topic
    here so you are better off asking in a group devoted to your system or
    one you understand.") did you not comprehend.


    <<Remove the del for email>>
     
    Barry Schwarz, Oct 14, 2003
    #14
  15. On Mon, 13 Oct 2003 12:58:10 +0100, Kevin Bracey
    <> wrote:

    >In message <bmcle8$va0$0@216.39.134.220>
    > Barry Schwarz <> wrote:
    >
    >> On Mon, 13 Oct 2003 00:33:44 +0530, Ravi <> wrote:
    >>
    >> >I understand this. But I want a proper example clearly
    >> >telling me the use of declaring a variable volatile etc.

    >>
    >> Any proper example would probably be implementation dependent. You
    >> haven't told us what system you are using or which other ones you are
    >> familiar enough with to understand the example if presented.
    >>
    >> And if you did it would be off-topic here so you are better off asking
    >> in a group devoted to your system or one you understand.

    >
    >Blimey, we're all being unhelpful today, aren't we? A whole keyword pretty
    >much declared off-topic...


    Do you enjoy taking things out of context or is it just a bad habit?
    After several examples, the OP still wanted a "proper" one that
    related to something he was familiar with. If you have one that is
    not implementation specific and not a duplicate of one already
    provided, by all means let us see it. The memory mapped I/O idea was
    previously discussed and already rejected by the OP as not
    sufficiently informative.


    <<Remove the del for email>>
     
    Barry Schwarz, Oct 14, 2003
    #15
  16. Ravi

    Dan Pop Guest

    In <bmfn99$dio$1@216.39.134.185> Barry Schwarz <> writes:

    >On Mon, 13 Oct 2003 13:53:58 +0530, Ravi <> wrote:
    >
    >>On 12 Oct 2003 22:43:20 GMT, Barry Schwarz
    >><> wrote:
    >>

    >snip
    >>>Any proper example would probably be implementation dependent. You
    >>>haven't told us what system you are using or which other ones you are
    >>>familiar enough with to understand the example if presented.

    >>
    >>Linux, Windows or DOS.
    >>
    >>TIA.

    >
    >Your not welcome. What part of the next paragraph which you
    >conveniently cut out (which said "And if you did it would be off-topic
    >here so you are better off asking in a group devoted to your system or
    >one you understand.") did you not comprehend.


    What part of my example was implementation dependent?

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Oct 14, 2003
    #16
  17. Ravi

    Alex Guest

    "Dan Pop" <> wrote in message
    news:bmgqb1$h6i$...
    > What part of my example was implementation dependent?


    Aren't all signal types are implementation defined?

    Alex
     
    Alex, Oct 14, 2003
    #17
  18. (Dan Pop) wrote:

    >>>On 12 Oct 2003 22:43:20 GMT, Barry Schwarz
    >>><> wrote:

    <snip>
    >>>>Any proper example would probably be implementation dependent. You
    >>>>haven't told us what system you are using or which other ones you are
    >>>>familiar enough with to understand the example if presented.

    <snip>
    >
    >What part of my example was implementation dependent?


    OK, I'm ready to burn my fingers by possibly typing nonsense:

    IIRC, according to ISO/IEC 9899:1999 7.14 and 7.14.1

    signal(SIGINT, handler);

    has several implementation defined aspects.

    Regards
    --
    Irrwahn,
    meanwhile checking his fire-extinguisher.
     
    Irrwahn Grausewitz, Oct 14, 2003
    #18
  19. On 14 Oct 2003 12:31:29 GMT, (Dan Pop) wrote:

    >In <bmfn99$dio$1@216.39.134.185> Barry Schwarz <> writes:
    >
    >>On Mon, 13 Oct 2003 13:53:58 +0530, Ravi <> wrote:
    >>
    >>>On 12 Oct 2003 22:43:20 GMT, Barry Schwarz
    >>><> wrote:
    >>>

    >>snip
    >>>>Any proper example would probably be implementation dependent. You
    >>>>haven't told us what system you are using or which other ones you are
    >>>>familiar enough with to understand the example if presented.
    >>>
    >>>Linux, Windows or DOS.
    >>>
    >>>TIA.

    >>
    >>Your not welcome. What part of the next paragraph which you
    >>conveniently cut out (which said "And if you did it would be off-topic
    >>here so you are better off asking in a group devoted to your system or
    >>one you understand.") did you not comprehend.

    >
    >What part of my example was implementation dependent?
    >
    >Dan


    Dan

    I responded to Ravi. I don't recall any text in his message relating
    to anything you did.


    <<Remove the del for email>>
     
    Barry Schwarz, Oct 15, 2003
    #19
  20. Barry Schwarz <> wrote:

    >On 14 Oct 2003 12:31:29 GMT, (Dan Pop) wrote:
    >
    >>>>On 12 Oct 2003 22:43:20 GMT, Barry Schwarz
    >>>><> wrote:
    >>>>>Any proper example would probably be implementation dependent. You
    >>>>>haven't told us what system you are using or which other ones you are
    >>>>>familiar enough with to understand the example if presented.

    <snip>
    >>What part of my example was implementation dependent?
    >>

    >I responded to Ravi. I don't recall any text in his message relating
    >to anything you did.


    I assume he was aiming at your claim that "Any proper example would
    probably be implementation dependent." and the fact that he gave such
    an example in his reply to the OP (M.ID bmeg7g$jsh$ ).

    Regards
    --
    Irrwahn
    ()
     
    Irrwahn Grausewitz, Oct 15, 2003
    #20
    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,479
    Knute Johnson
    Jul 3, 2003
  2. Doug Pardee

    Re: Volatile?

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

    Can volatile be trusted?

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

Share This Page