difference between volatile and none volatile variable?

Thread Starter

@vajra

Joined May 2, 2018
145
I found on internet that volatile variable tells the compiler that volatile variable can change its contents outside but I don't really understand difference between volatile and none volatile variable? why the volatile keyword should be used.
Code:
#include<stdio.h>

int main(void)
{
    volatile int x;
             int y;
    x = 3;
    x++;
    y = 3;
    y++;   
    printf("volatile is %d\n", x);
    printf("None volatile is %d\n", y);   
    return 0;
}
 

peterdeco

Joined Oct 8, 2019
27
In microcontrollers, volatile variables can become a random value upon power up. Non volatile variable values are sometimes written to eeprom where their value remains the same. They can also be written into program memory, but sometimes that gets risky unless you know exactly where to write them.
 

BobTPH

Joined Jun 5, 2013
2,057
volatile in C means that the variable might be accessed outside the normal flow of the program.

The typical example is a variable used in an interrupt handler. The interrupt might occur between any two instructions in the program, and the interrupt handler might use or even change its value.

Why does this matter?

Consider the following program:

Code:
int x:
Int main(void)
{
    x = 1;
    y = x + 1;
    ...
}
If x was not volatile, the compiler could simply assign 2 to y instead of doing the addition of x and 1.

But if x can be changed in an interrupt handler, it cannot rely on x still being one. So it must actually do the addition before assigning to y.

Bob
 

nsaspook

Joined Aug 27, 2009
6,506
The key point to understand with regard to volatile is that its purpose is to suppress optimization.
Compiler people tend to think that people want smart compilers. I as a
compiler power-user can emphatically tell you that that isn't true. People
want _reliable_ compilers, and they want compilers that can be told to just
get the hell out of the way when needed. It's not often you need to, but
when you need to, you _really_ need to.
Linus Torvalds

Volatile in C is a way (a builtin #Pragma) of dumbing down the C compiler optimizer in specific variables, code blocks and procedures. Hey, don't be so smart when deciding what to change in my special program! Typically with most compilers, if you completely (watch out for modern gcc optimization options) turn off the optimization stages you can program X (interrupt handlers, port pins, RTOS context changes) without Volatile. Now, usually you don't want to, because it makes for larger/slower binaries but it's a useful method (debug-friendly) for tracking down bugs related to memory access issues. Volatile is not a Memory_barrier. It sometimes acts as one on simple hardware but it's not guaranteed.

https://barrgroup.com/Embedded-Systems/How-To/C-Volatile-Keyword

Some compilers allow you to implicitly declare all variables as volatile. Resist this temptation, since it is essentially a substitute for thought. It also leads to potentially less efficient code.
 
Last edited:

MrSoftware

Joined Oct 29, 2013
1,576
There is another thread here about volatile. It has to do with compiler optimization and variables that might change outside the main program flow, such as with interrupts. For example:

C:
int x:



void myInterruptRoutine(void)
{
  x = 0;
}

Int main(void)
{
    x = 1;
  while(x > 0)
     sleep(10);
  printf("All done.");   
}
In the above code, if x is not declared as volatile, the compiler may optimize the while() loop to just be while(1), or may not see the reason to go and read the memory address for x at every loop because it does not see any place for x to change in the local scope and will just use the cached value from a register. So even if the memory allocated for x is updated properly in the interrupt routine, the value will never be updated in the while() and it will loop forever. This is a speed optimization, it takes clock cycles to go get values from memory so the compiler will eliminate any fetches from memory that it feels are unnecessary (depending on compiler settings). If you declare x to be volatile, then on every single iteration of the while() loop, the processor will go and fetch the value of x from its address in memory. So when x is changed during the interrupt, the value will be read and the program will run as expected.

When an interrupt routine is called, the processor immediately stops what it is doing, runs the interrupt routine, then returns to where it left off as if nothing happened. So unless the memory at address x is read again, the processor will have no idea that the value changed.
 
Top