Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
01/23/03 05:39
Read: times


 
#36976 - RE: volatile keyword
The volatile key word is applied to any variable that when its value is written or referenced in C code, that the actual value in that variable may change for some reason before the next time the variable is referenced by the C code.

For example..if you had...

int x;
int y;

void func(void)
{
x=13;
y=45;
y=y*2;
y=y+some_subroutine(y);
y=y+x;
}

....if there was a chance that the X value could change by the time you got the last statement then you would want to declare x with the volatile attribute. The precise reason this is important is because with code like above the compiler may have decided to keep the 13 value of x in a register down through the code sequence and then used that registered value instead of going back to the x memory location for the value. A compiler with no optimization would keep the x & y memory locations up to date statement by statement. However the modern compilers today, in attempting to make code smaller and smaller will try to keep intermediate values in local storage (ie registers) across several statements in order to make the code more efficient and smaller. When you apply the volatile keyword to a variable you are telling the compiler to go back to reference the actual storage for the variable on a statement by statement basis.

Michael Karas


List of 18 messages in thread
TopicAuthorDate
volatile keyword            01/01/70 00:00      
RE: volatile keyword            01/01/70 00:00      
RE: volatile keyword            01/01/70 00:00      
RE: volatile keyword            01/01/70 00:00      
RE: volatile keyword            01/01/70 00:00      
RE: volatile keyword            01/01/70 00:00      
RE: volatile keyword            01/01/70 00:00      
RE: volatile keyword            01/01/70 00:00      
RE: volatile keyword            01/01/70 00:00      
RE: volatile keyword            01/01/70 00:00      
RE: volatile keyword            01/01/70 00:00      
RE: volatile keyword            01/01/70 00:00      
RE: volatile keyword            01/01/70 00:00      
RE: volatile keyword            01/01/70 00:00      
RE: volatile keyword            01/01/70 00:00      
RE: volatile keyword            01/01/70 00:00      
RE: volatile keyword            01/01/70 00:00      
volatile keyword and not-so-optimizer            01/01/70 00:00      

Back to Subject List