I have used a static global variable and a static volatile variable in file scope,
both are updated by an ISR and a main loop and main loop checks the value of the variable.
here during optimization neither the global variable nor the volatile variable are optimized. So instead of using a volatile variable a global variable solves the problem.
So is it good to use global variable instead of volatile?
Any specific reason to use static volatile??
Any example program would be appreciable.
Thanks in advance..
They are different things. I’m not an expert in volatile semantics. But i think it makes sense what is described here.
Global
Global just means the identifier in question is declared at file-scope. There are different scopes, called function (where goto-labels are defined in), file (where globals reside), block (where normal local variables reside), and function prototype (where function parameters reside). This concept just exist to structure the visibility of identifiers. It doesn’t have anything to do with optimizations.
Static
staticis a storage duration (we won’t look at that here) and a way to give a name declared within file scope internal linkage. This can be done for functions or objects only required within one translation unit. A typical example might be ahelpfunction printing out the accepted parameters, and which is only called from themainfunction defined in the same.cfile.6.2.2/2 in a C99 draft:
Internal linkage means that the identifier is not visible outside the current translation unit (like the
helpfunction of above).Volatile
Volatile is a different thing: (6.7.3/6)
The Standard provides an excellent example for an example where
volatilewould be redundant (5.1.2.3/8):Sequence points are points where the effect of side effects concerning the abstract machine are completed (i.e external conditions like memory cell values are not included). Between the right and the left of
&&and||, after;and returning from a function call are sequence points for example.The abstract semantics is what the compiler can deduce from seeing only the sequence of code within a particular program. Effects of optimizations are irrelevant here. actual semantics include the effect of side effects done by writing to objects (for example, changing of memory cells). Qualifying an object as volatile means one always gets the value of an object straight from memory (‘as modified by the unknown factors’). The Standard doesn’t mention threads anywhere, and if you must rely on the order of changes, or on atomicity of operations, you should use platform dependent ways to ensure that.
For an easy to understand overview, intel has a great article about it here.
What should i do now?
Keep declaring your file-scope (global) data as volatile. Global data in itself does not mean the variables’ value will equal to the value stored in memory. And static does only make your objects local to the current translation unit (the current
.cfiles and all other files #include’ed by it).