Skip to main content

Volatile

Volatile Keyword is important keyword in c programing language.  volatile  keyword is one type of qualifier in c programming languages. But, when it comes to writing code especially in Embedded programming the use of “volatile” keyword is very often.Volatile Keyword in Embedded System.

But now the question is,

  • What is it (volatile)?
  • What it does?
  • Why do we need it?
  • Does my program/code really need this keyword?

All these and more I will cover in this article.

What does volatile mean?

Ok, let’s find out the meaning of volatile in dictionary.
VOLATILE means: – UNSTABLE, UNPREDICTABLE…etc.
So, the basic meaning of volatile is we can’t predict what is going to happen next.
Now,

What is the significance of volatile keyword?

The significance of volatile keyword in programming language is to inform/tell the compiler not to pre-predict/assume/believe/presume the value of the particular variable which has been declared as volatile.

What is code optimization?

I think this is the particular section is missing everywhere. So now we will see, what is code optimization….
Look at the figure 1 and Figure 2, below.
Suppose:-
Figure 1
int x = 0;
main()
{
if (x == 0) /* This condition is always true */
{
printf(“x = 0 \n”);
}
else /*Else part will be optimized because x will never ever other than 0*/
{
printf(“x != 0 \n”);
}
}
Figure 2
volatile int x; /* volatile Keyword*/
main()
{
x = 0;
if (x == 0)
{
printf(“x = 0 \n”);
}
else
/*Else part will never ever be optimized
because the variable is declared as volatile*/
{
printf(“x != 0 \n”);
}
In fig-1, compiler will optimize the code it will ignore the else part, because the variable “x” will never ever become other than 0.
In fig-2, the compiler will never ever optimized the code (else part) because, by declaring x as volatile compiler comes to know that this variable can change at any point of time. So compiler does not ignore the else part.

Why/When do we need it?

In following case we need to use volatile variable.




  • Memory-mapped peripheral registers
  • Global variables modified by an interrupt service routine
  • Global variables within a multi-threaded application

If we do not use volatile qualifier the following problems may arise:




  • Code that works fine-until you turn optimization on
  • Code that works fine-as long as interrupts are disabled
  • Flaky hardware drivers
  • Tasks that work fine in isolation-yet crash when another task is enabled

e.g
static int var; void test(void) {
var = 1; while (var != 10) continue;
}
The above code sets the value in var to 1. It then starts to poll that value in a loop until the value of var becomes 10.
An optimizing compiler will notice that no other code can possibly change the value stored in ‘var’, and therefore assume that it will remain equal to 0 at all times. The compiler will then replace the function body with an infinite loop,similar to this:
void test_opt(void) {
var = 0; while (TRUE)/*#define TRUE 1*/ continue; }
How do we declare a volatile variable?
Include the keyword volatile before or after the data type in the variable.
volatile int var;
or
int volatile var;
Pointer to a volatile variable
volatile int * var;
int volatile * var;
Above statements implicate ‘var’ is a pointer to a volatile integer.
Volatile pointers to non-volatile variables
int * volatile var; –> Here var is a volatile pointer to a non-volatile variable/object. This type of pointer are very rarely used in embedded programming.
Volatile pointers to volatile variables
int volatile * volatile var;
If we qualify a struct or union with a volatile qualifier, then the entire contents of the struct/union becomes volatile. We can also apply the volatile qualifier to the individual members of the struct/union.
Does my program/code really need this keyword?
As we have already seen above, when do we need volatile keyword, then below are some cases which really makes you understand.


Conclusion
1. A volatile variable can be changed by the background routine of preprocessor. This background routine may be interrupt signals by microprocessor, threads, real times clocks etc.
2. In simple word we can say a value volatile variable which has stored in the memory can be by any external sources.
3. Whenever compiler encounter any reference of volatile variable is always load the value of variable from memory so that if any external source has modified the value in the memory complier will get its updated value.
4. Working principle of volatile variable is opposite to the register variable in c. Hence volatile variables take more execution time than non-volatile variables.

Comments

Popular posts from this blog

CAN INTERVIEW QUESTIONS

Qualifiers and Modifier in C

RTOS Real time operating system interview questions

CAN INTERVIEW QUESTIONS 2

What is UDS protocol

Memory mapping in c

TOP IOT PLATFORMS