Introduction
When you want to refer a variable, many times you can allocate fast memory in the form of a register to that variable. For variables such as loop counters, register allocation is done. The processor has memory in the form of register for its temporary storage. The access time of the register is much less than main memory. That is the reason that register allocation provides more speed. But the processor has a limited number of registers. So the register declaration acts as a directive; it does not guarantee the allocation of a register for storing value of that variable.
Program
#include
main()
{
register int i = 0; \\ A
for( i=0;i<2;i++)
{
printf("value of i is %d\n",i);
}
}
Explanation
Here the register allocation directive is given for variable i. During execution, i will be allocated a register if it is available; otherwise, i will receive normal memory allocations.
You can use a register directive only for variables of the automatic storage class, not for global variables.
Generally, you can use register storage for int or char data types.
Note You cannot use register allocation for global variables because memory is allocated to the global variable at the beginning of the program execution. At that time, it is not certain which function is invoked and which register is used. Function code may use the register internally, but it also has access to a global variable, which might also use the same register. This leads to contradiction, so global register variables are not allowed.
Points to Remember
Register allocation is done for faster access, generally for loop counters.
You cannot declare global register variables.
When you want to refer a variable, many times you can allocate fast memory in the form of a register to that variable. For variables such as loop counters, register allocation is done. The processor has memory in the form of register for its temporary storage. The access time of the register is much less than main memory. That is the reason that register allocation provides more speed. But the processor has a limited number of registers. So the register declaration acts as a directive; it does not guarantee the allocation of a register for storing value of that variable.
Program
#include
No comments:
Post a Comment