what is volatile variable in C 1. Volatile is a global - TopicsExpress



          

what is volatile variable in C 1. Volatile is a global variable. 2. takes 1 byte of instruction 3. Compiler cannot optimise the variable. 4. type casting is not allowed. 5. Value can be modified by ports, Hardware, interrupts,serial ports only. 6. Permanent memory location is allocated for it. When we use the word Optimise, we mean that the variable in the memory can be changed only by the compiler whenever the code is executed. Volatile modifiers cannot be optimised by the compiler, During linking process the code is allocated physical memory in the internal memory. so during the link process( generation of .lnk file during compilation) these variables are placed in the heap instead of main memory. Compiler cannot modify the variable until unless a copy is copied to RAM for execution. So the compiler allocates a different memory location for the variable. These are called un-optimised location. the variables in this location are not dependent on the compiler to change the value. instead interrupt, ports, serial, hardware are given permission to access these variables when ever they raise a request. When a memory is optimised, then the life of that variable is limited only till the function executes then it is destroyed, but volatile are not destroyed, but keep value in it till there is any change done by external entities. Whenever these variables are accessed only the last updated value is seen in the register. Uploading a example after this. the best example is RTC(Real Time Clock). in the PC. even when the PC is shut down , and later restarted, the clock updates the latest current time. eg: volatile char time; void update(void) { time =time+1; } void main() { time=0; while(time
Posted on: Mon, 28 Oct 2013 06:32:19 +0000

Trending Topics



Recently Viewed Topics




© 2015