what is volatile variable in C

Status
Not open for further replies.

sagar474

Full Member level 5
Joined
Oct 18, 2009
Messages
285
Helped
5
Reputation
10
Reaction score
5
Trophy points
1,318
Location
India,kakinada
Activity points
3,123
what is the use of declaring a variable as volatile
what can i do with this

volatile int VI;

can i have an example.
 

1. Volatile is a global variable.
2. takes 1 byte of instruction
3. Compiler cannot optimise the variable.
4. type casting is not allowed.
5. Value can be modified by ports, Hardware, interrupts,serial ports only.
6. Permanent memory location is allocated for it.


When we use the word Optimise, we mean that the variable in the memory can be changed only by the compiler whenever the code is executed. Volatile modifiers cannot be optimised by the compiler, During linking process the code is allocated physical memory in the internal memory. so during the link process( generation of .lnk file during compilation) these variables are placed in the heap instead of main memory. Compiler cannot modify the variable until unless a copy is copied to RAM for execution. So the compiler allocates a different memory location for the variable. These are called un-optimised location. the variables in this location are not dependent on the compiler to change the value. instead interrupt, ports, serial, hardware are given permission to access these variables when ever they raise a request.
When a memory is optimised, then the life of that variable is limited only till the function executes then it is destroyed, but volatile are not destroyed, but keep value in it till there is any change done by external entities. Whenever these variables are accessed only the last updated value is seen in the register.

Uploading a example after this. the best example is RTC(Real Time Clock). in the PC. even when the PC is shut down , and later restarted, the clock updates the latest current time.

eg:
volatile char time;
void update(void)
{
time =time+1;
}

void main()
{
time=0;
while(time<100);
}


without volatile modifier the compiler looks at this as 2 different statements.
1. time =0;
2. while(time<100);

since time =0; 0<100, so the loop always stay in the same line till the condition is true.
Time never reach 100. So when memory is optimised then it can be changed only through execution of explicit statement which modify the value. in the above case it does not change.

if we use volatile modifier, this disables optimisation, forcing the program to fetch a new value from the variable every time variable is accessed.

hope this answers your question. if not then i can still continue my explaination in my next post. let me know if you got anything from this.

Always remember you cannot simulate the condition of volatile in c51 or any compiler as Heap memory cannot be simulated in keil. it can be seen only on hardware.
 
yes your explanation make me understand and get some idea about volatile variable.

but i have a question
can a any other program running on the same machine can change the volatile variable? how?

why it should be stored in heap?
 

Hi Sagar,

Well ckshivaram beat me to it.

So rather than give you another long explanation, I'll give you a link with examples:

How to Use C's volatile Keyword

Quote from the site:


Let me know if you need more examples, I have plenty.
 
it need not be always heap... as many controllers cannot have heap..... a memory which is not optimised...... and its not other program that can access...

the example is the RTC of the computer.... a variable that can be accessed by not the program but by interrupts , hardware ports....

Even when the power goes the internal battery runs the rtc... and when you re-power on, you read the last updated memory only not before that... so it stores the last update value....
Since embedded systems are for specific applications there would not be parallel programs running...
Yes same variable if declared in ISR routine or for a port or any other hardware can modify it, but user would not know who changed it....
So its programmer developer to make use of this feature properly......

Hope i answer you.. or still you need clarification......
 
However, the qualifier keyword volatile is generally nonportable, meaning it's effect on compilation varies from one compiler to the next, as well as from one platform to the next.

Obviously, if you are coding for a MCU the first instance cited in the quote above would occur routinely. Most compilers for embedded devices optimize in such a way as to take this into account, thus the volatile qualifier is used only occasionally.

If your coding for x86, the use of the qualifier volatile maybe more of a necessity in some circumstances, like accessing a memory mapped parallel port, etc.
 


if we are coding for x86 with operating system. then we can have parallel programs.
then if there any possibility to change a volatile variable by other program running in parallel ?
 

irrespective of processor and environment.. the developer should know about the variables declared as volatile and the functions accessing those variable and the conditions under which they are accessed, and In case of multi program environment , concept of shared memory, safe guarding the variable from other programs comes into picture....
 
Whenever a value of the variable is varied by an external event (Ports , ISR and status Registers) and not by the program, that variable must be declared as volatile. If compiler setting of code optimization is Off then no need to declare as volatile. But best practice should be using of volatile in such above mentioned conditions.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…