Understanding “volatile” qualifier in C

Thread Starter

Dadu@

Joined Feb 4, 2022
155
Hello Forum!

I am trying to understand what is the advantage of using volatile keyword in standard C. The volatile keyword in C language confuses me a lot. definition say that a volatile keyword indicates that the value of the variable can be changed from outside at any time. I often see that the volatile keyword is used inside interrupt service routine.

Let's say i have a c program in which I declare a global variable X. I initialize X with 0 in main function. But when an interrupt occurs, the value of this X changes.

What happens if I don't use volatile keyword in the program, will the compiler remember the current value of X if it has been modified by interrupt?
 

Papabravo

Joined Feb 24, 2006
22,082
Hello Forum!

I am trying to understand what is the advantage of using volatile keyword in standard C. The volatile keyword in C language confuses me a lot. definition say that a volatile keyword indicates that the value of the variable can be changed from outside at any time. I often see that the volatile keyword is used inside interrupt service routine.

Let's say i have a c program in which I declare a global variable X. I initialize X with 0 in main function. But when an interrupt occurs, the value of this X changes.

What happens if I don't use volatile keyword in the program, will the compiler remember the current value of X if it has been modified by interrupt?
It is unlikely. Volatile is a suggestion to the compiler that it should not depend on the value of the variable being the same from one read access to the next.
 

xox

Joined Sep 8, 2017
936
Hello Forum!


I am trying to understand what is the advantage of using volatile keyword in standard C. The volatile keyword in C language confuses me a lot. definition say that a volatile keyword indicates that the value of the variable can be changed from outside at any time. I often see that the volatile keyword is used inside interrupt service routine.


Let's say i have a c program in which I declare a global variable X. I initialize X with 0 in main function. But when an interrupt occurs, the value of this X changes.


What happens if I don't use volatile keyword in the program, will the compiler remember the current value of X if it has been modified by interrupt?

Certain compiler optimizations can result in invalid machine code being generated by the compiler. Without any optimizations, the volatile keyword is more or less just a decoration. With optimizations however it becomes essential. But then only necessary for variables that have some REAL measure of "hidden semantics" going on. Forming a pointer to a screen buffer, for example, which is constantly being accessed at the hardware level. That pointer should MOST DEFINITELY be declared volatile. On the other hand, in the case where a global counter variable is being used to keep track of a player's score, it isn't really warranted.
 

BobTPH

Joined Jun 5, 2013
11,516
Hello Forum!

I am trying to understand what is the advantage of using volatile keyword in standard C. The volatile keyword in C language confuses me a lot. definition say that a volatile keyword indicates that the value of the variable can be changed from outside at any time. I often see that the volatile keyword is used inside interrupt service routine.

Let's say i have a c program in which I declare a global variable X. I initialize X with 0 in main function. But when an interrupt occurs, the value of this X changes.

What happens if I don't use volatile keyword in the program, will the compiler remember the current value of X if it has been modified by interrupt?
Yes, an optimizing compiler might well do that.

A typical optimization done by the compiler is to remove a calculation from a loop if the variables used are not changed in the loop, computing it once before the loop starts.

B
 

Thread Starter

Dadu@

Joined Feb 4, 2022
155
It is unlikely. Volatile is a suggestion to the compiler that it should not depend on the value of the variable being the same from one read access to the next.
Can anyone demonstrate how compiler work with and without volatile in following examples

without volatile
C:
int flag;

int main (void)
{
   flag = 0;
    ...
   while(flag == 1)
   {
     
   }
 
   
    return 0;
}

ISR()
{
    flag = 1;
}
with volatile
C:
volatile int flag;

int main (void)
{
   flag = 0;
    ...
   while(flag == 1)
   {
     
   }
 
   
    return 0;
}

ISR()
{
    flag = 1;
}
 

BobTPH

Joined Jun 5, 2013
11,516
Without volatile, s typical optimizing compiler would go through the following steps.

1. It would see that flag must be 0 at the test.

2. It would change the result of the compare to be the constant false.

3. It would then change the branch at the top of the loop to an unconditional branch, since it is always false.

4. It would then notice that all the code in the loop was unreachable and remove it.

With the volatile keyword, it cannot make the assumption in 1, so none if the other steps are taken and the code is left as it was written.

Bob
 
Top