• C

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
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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;

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 )

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
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).
I must have misunderstood your question, because I didn't think the other posts answered it heh.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.