• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1525
  • Last Modified:

volatile variable

if i declare a variable ,which is local to a function,to  be volatile where it will be stored
what is the differnece between a global volatile and static volatile
this question was asked in an interview
0
prasad2315
Asked:
prasad2315
  • 2
2 Solutions
 
Doc_McAlisterCommented:
volatile means that you can't trust it.  That you can't optimize/cache it.  It might change at anytime with no action being done by the code around it. You need to check to see that the variable is still what you think it is every time you try to use it.

Basically, it means the compiler can't get clever and try to optimize things for you involving that variable.


Now a local, non-static, variable will almost never need to be volatile.  Each time the function is run that runthrough gets its own private memory for the variable and nothing else can see it.  So nothing else can *change* it.  It won't change, no point in making it volatile.

A local *static* variable is a variable where a set place in memory is reserved that every instance of the function uses.  

void myFunction()
{
    static int i = 0;
    cout << i << endl;
    i++;
}

Because i is static its space in memory is fixed.  And it hangs around after the function is called!  So the first time you call it you get 0, but the second time 1!  and the third time 2!  etc.

Now without the static keyword, the compiler could say "hey, i is created, set to 0, then printed then incremented.  I can just replace all three lines with "cout << 0 << endl" and it'll do the same thing!" But with the static keyword it can't do that.  

Now most compilers are smart enough to realize that in this simple case and not try to optimize it.  But sometimes they aren't on the trolly and try to optimize something that they can't safely do so.  And you stop them by making the variable in question volatile.

global variables have the same sharing issues.  Say you set a global flag to false and then do this:

while ( flag == false )
{
    sleep(2);
}

It's going to sleep forever!  ... well say you have an interrupt handler set so pressing any key sets the flag to true.  You are ok.  Unless your optimizer doesn't realize this and optimizes it to while (true).  But set the flag to volatile and the optimizer knows it can't make any assumptions.
0
 
evilrixSenior Software Engineer (Avast)Commented:
The use of volatile is, as Doc_McAlister has pointed out, is quite specialized and should only be used if necessary. The reason for this is its use prevents compiler optimizations and can, as such, mean your code performance is degraded. In essence, unless you specifically want to prevent optimization (such as register caching) of a value (for example if you are writing multi-treaded code) there is no need to use this specifier.

The following article discusses volatile and its use in some detail.
http://www.programmersheaven.com/articles/pathak/article1.htm
0
 
Infinity08Commented:
As Doc_McAlister said, the volatile qualifier means that the compiler is not allowed to (aggressively) optimize that variable.

When you declare a variable volatile, you basically say that the variable can be modified "in ways unknown to the compiler" or can have "side effects unknown to the compiler". For example, some hardware might modify it (which is often true in device drivers) if it's a memory-mapped I/O port. Or it might be modified by an asynchronous interrupt (a user program abort for example).

However, there are some very valid situations, in which using the volatile qualifier for local (automatic) variables is needed. One example is to access another volatile object (through a pointer for example).


Now, to answer your questions :

>> if i declare a variable ,which is local to a function,to  be volatile where it will be stored

volatile is a type qualifier - it doesn't imply anything about storage. So, it will end up on the stack like the other local variables.


>> what is the differnece between a global volatile and static volatile

global and static are not mutually exclusive, so I'm not sure what they're getting at with this question. In any case, all of the above still applies, and global/static are related to the scope/storage of the variables, while volatile is related to the usage of the variable (especially with regards to optimization).
0
 
Infinity08Commented:
I must have misunderstood your question, because I didn't think the other posts answered it heh.
0

Featured Post

Live webcast with Pinal Dave

Pinal Dave will teach you tricks to help identify the real root cause of database problems rather than red herrings. Attendees will learn scripts that they can use in their environment to immediately figure out their performance Blame Shifters and fix them quickly.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now