Thread: volatile variable c program

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    22

    volatile variable c program

    I have been spent so much time on google to find complete code for volatile variable in c language. I am so confused by reading a lot of theoretical answer

    Definition : "The volatile keyword tells the compiler that the value of the variable may change at any time"

    I don't get any idea form definition If possible can someone give link to complete code or explain the use of volatile keyword in code?

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,675
    It means that the value of the variable may change at any time, possibly from outside the program in question. For example, the variable might be a pointer to a hardware memory location that is set/changed by something that has nothing to do with the program. An interrupt request handler might change it or someone pressing a key on the keyboard etc etc. So by telling the compiler that the variable is volatile you're telling it something along the lines of "do not optimize this away or make any assumptions about the value based on analysis". E.g. an optimizing compiler might see that you don't change the value of 'my_variable' during a loop and also see that you keep checking its value and, since the value of 'my_variable' is not changed within the loop it might decide to optimize that check away because it never changes anyway. But if you say that 'my_variable_' is volatile you're letting the compiler that it can actually change (it's not an invariant) because factors outside of your program and the immediate context can change it (e.g. someone pressing a key).

    For example (this is pretty contrived, but it might serve)

    Code:
    int *key = 0x00fffffff;
    int prev_value, j = 0;
    
    prev_value = *key;
    for (int i = 0; i < 500; ++i) {
        if (*key != prev_key) {
             ++j;
             prev_key = *key;
        }
    }
    since key is not declared as pointing to something that you've marked as volatile the compiler could optimise away the entire loop (because the program does not change *key then nothing changes in the loop -- i.e. *key != prev_key can never be true so there is no need for the loop; the loop does nothing because *key is invariant/never changes). If you have volatile int *key though you're telling the compiler to make no assumptions about the value it points to; it's volatile and can change even if it doesn't look like it from the code you've written.

    Edit:

    By changing the declaration of *key to volatile int *key = 0x00fffffff you're telling the compiler that it cannot make any assumption about the value that 'key' points to, so the compiler cannot optimise the loop away. In other words, it's saying (essentially) that this program is not changing the value at 0x00fffffff but something else (e.g. the hardware) can.
    Last edited by Hodor; 11-26-2019 at 03:11 AM.

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    22
    Quote Originally Posted by Hodor View Post
    It means that the value of the variable may change at any time, possibly from outside the program in question. .
    Hi Hodor

    I am still confuse. look at following code it will not compile it's just example of timer interrupt. assume interrupt generate at every 1 second

    Code:
    #include<microcontroller.h>
    
    volatile int TimerFlag = 0;
    
    int main(void) 
    { 
       
      while (!TimerFlag) // check TimerFlag  is not set 
      {
          //Task 1
          //Task 2
          //Task 3
    
           ..
         //Task 100
      }
    
    
      return 0;
    }
    
    
    ISR(void) 
    { 
      TimerFlag = 1; 
      //Do important task  
    
    
    }
    There is two two function in program main function and interrupt function. When interrupt routine is called processor immediately stop what it was doing and run the interrupt routine then return to where it left off

    Where does the variable change inside interrupt or outside the interrupt

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,675
    In your example 'TimerFlag' is set/changed in the function ISR(). By using volatile you've told the compiler that, yeah it might change not matter what you think. Without your compiler might get rid of that loop entirely (assuming that task 1, 2, 3 etc don't change it) or do some other optimisation that breaks things because given what you've written, and if you had not said TimerFlag is volatile, TimerFlag would not change within that loop). ISR(void) might not even be in your program. That's what the volatile keyword is for: it's you telling the compiler "hey, TimerFlag can be changed by something not in this context or outside of the program. Be careful". By using the volatile keyword you're telling the compiler to be very careful about assumptions it might otherwise make. So, yeah, your example is probably good: what calls ISR() and changes TimerFlag? Without the volatile keyword the C compiler might optimise the while loop away completely because TimerFlag does not change in that loop given the context (TimerLoop without the volatile is a loop invariant). No where in that loop does TimerFlag change. The volatile tells the compiler to not assume it's invariant (i.e. volatile means it might change even though context suggests it won't) and to be cautious about what it can and cannot do.

    Edit: it changes in the interrupt (which is why volatile is needed because it certainly doesn't change in the loop)
    Last edited by Hodor; 11-26-2019 at 07:29 AM.

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    591
    One example of what @Hodor probably meant by "By using the volatile keyword you're telling the compiler to be very careful about assumptions it might otherwise make":
    Code:
    int a;
    volatile int b;
    
    void f( void ) { a |= 0; }
    void g( void ) { b |= 0; }
    If you compile this with good optimization and take a look at f() ang g() code, you'll see:
    Code:
    f:
      ret
    
    g:
      mov eax,[b]
      mov [b],eax
      ret
    Notice ORing an integer with 0 do nothing... Since 'a' isn't volatile the compiler assumes it doen't change outside the code (by an interruption or if it is an controller register, for example), so there is no need to do the ORing... But 'b' is volatile, so the compiler don't assume the value will be the same always and the copy is made.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is volatile variable?
    By shansajid in forum C Programming
    Replies: 2
    Last Post: 02-14-2013, 07:21 AM
  2. VOLATILE vs Normal VARIABLE N C
    By cvr.09 in forum C Programming
    Replies: 23
    Last Post: 02-12-2013, 06:42 PM
  3. Volatile Variable.
    By nagavardna in forum C Programming
    Replies: 2
    Last Post: 04-19-2012, 03:34 AM
  4. Difference between global variable and volatile
    By karthik537 in forum C Programming
    Replies: 4
    Last Post: 02-29-2012, 11:28 AM
  5. Guaranteed read from volatile variable
    By cyberfish in forum C++ Programming
    Replies: 17
    Last Post: 08-15-2011, 09:00 AM

Tags for this Thread