Reputation: 11968
Few days back i had an interview but, still I am searching for the answer. I would like to understand the significance of using volatile keyword.
Find the code below: Two different scenario.
//project1
//File1.c
int abc;//Global variable
/*And this variable is getting used in some other files too.*/
if(abc == 3) //Say
{
printf("abc == 3");
}
else
{
printf("abc != 3");
}
/*So if or else part will not be optimized
because "abc" can not be predicted,
the value can chage at any point of time */
//Project2
//file1.c
volatile int abc;//Global variable with volatile keyword
/*And this variable is getting used in some other files too.*/
if(abc == 3) //Say
{
printf("abc == 3");
}
else
{
printf("abc != 3");
}
/*So if or else part will not be optimized
because "abc" can not be predicted as it is declared as volatile,
the value can chage at any point of time */
Why we should use volatile keyword instead?
Upvotes: 5
Views: 17296
Reputation: 340
I don't quite agree with the previous answers so here are my two cents.
By declaring a variable volatile you're telling the compiler that its value can change at any time and that it cannot make any assumptions about the variable's value, not even in two consecutive (assembler) instructions. In consequence any use of the variable must be done by accessing the actual variable and not a cached value.
In the code you provided there is no behavioural difference between abc being global, extern or volatile because if()/else only evaluates the variable once. There is a difference however if you change the else by a second if, as in:
if(abc == 3)
{
printf("abc == 3");
}
if (abc != 3)
{
printf("abc != 3");
}
If abc is NOT declared volatile, the compiler may optimize the second if and replace it by an else statement, in consequence abc would only be read and evaluated once, meaning either the first xor the second prints will be executed (but not both and also not none).
If abc IS declared volatile (no matter if local, global or extern), the compiler is forced to evaluate abc twice because its value could have changed in between. This means that any of the two prints, or both, or none, may be executed.
Extern is a different story entirely. All it does is tell the compiler that the variable has been defined in another file (whose address will be provided at link time). If the compiler can predict that the value of abc won't change between the two if statements (it doesn't matter how the compiler might be able to), then it may still optimize the second if into an else and reduce the two abc evaluations to one.
Upvotes: 2
Reputation: 1
Essentially, volatile is used to indicate that a variable's value will be modified by different threads.
Declaring a volatile Java variable means:
The value of this variable will never be cached thread-locally: all reads and writes will go straight to "main memory";
Access to the variable acts as though it is enclosed in a synchronized block, synchronized on itself.
Extern essentially means that all modules can use the defined variable.
Upvotes: -1
Reputation: 5637
As Tony Delroy explained in its comment, extern and volatile are quite different.
Volatile keyword protects your variable from being aggressively optimised. An optimised variable can be invisible to other threads and never reach main memory. Sometimes, compiler can even squeeze entirely a variable if it's not needed. Compiler base its guess with your source code as its only input. Sometimes, there is some external events which can change your variable value. It could be an hardware device or an other process, for instance.
=> Concretely, compiler disables some optimisations to this variable, so it can behave as you want it to.
Extern is not about cache vs memory. Extern is just about accessing a variable which lives in an other object files. See this short example of what kind of assembly code is generated for an extern access. Those extern variables are optimised in their own object file as far as it's possible. There's no question about to protect it or not.
=> Concretely, compiler indicates an external reference needing to be solved at link time
Upvotes: 7
Reputation: 48813
volatile in declaration or prototype say always load/store value from/to memory regardless local, static or extern this vlaue (but in case of local it is not always meaningful).
Also using volatile keyword in regular source are untypically. It is useful only for hardware which map hardware registers to memory (like in ARM architecture), in special cases of kernel/driver development.
If you use volatile in GUI or ecommerce code you probably wrong...
Upvotes: 4
Reputation:
volatile
usually means one or more of the following:
volatile
means that throughout the lifetime of the program there are two (or more) reads R1 and R2 of the variable, and some other event happening inbetween R1 and R2 will change the variable outside of the normal flow of execution.
extern
means that the variable has been defined somewhere elsewhere and that the program is reusing that definition.
Upvotes: 2