Tuesday 14 May 2013

Register and Volatile Variable

Let us discuss why do we need register variable and how it helps.

- declaring register variable advices to compiler that this variable is heavily used
- register variable are to be placed in machine registers
- register variables can only be applied to automatic/local scope of variables
- register variables also depends on the hardware, since it directly deal with machine registers
- in case if compiler couldn't able to allot machine register for requesting register variable then the variable will be scoped as "automatic/local" variables.

What is Volatile ?  why do we need them ?

- volatile variable declaration advices compiler that don't optimize this variable, means always fetch the value from the harddisk / main memory
- for ex: there are the cases that values could changed intermediately by some other task where as current running would be using the value which was stored in virtual memory .. so the changed value on the disk it not read in the execution == this is how the compiler optimize the program , note  changing values in run time differs from reading the disk / main memory
- to do so  , declare the variable as volatile so that compiler wont optimize it , and it will always fetch from main memory , though it is inefficient
-- use register /  volatile is there is a critical requirement , otherwise it's my opinion to not use of them ..


No comments:

Post a Comment