Register storage class specifiers in c
The register storage specifier originally applied only to variables of type int, char, or pointer types. However, in Standard C, register's definition has been broadened so that it applies to any type of variable. Originally, the register specifier requested that the compiler keep the value of a variable in a register of the CPU rather than in memory, where normal variables are stored. This meant that operations on a register variable could occur much faster than on a normal variable because the register variable was actually held in the CPU and did not require a memory access to determine or modify its value. Today, the definition of register has been greatly expanded and it now may be applied to any type of variable. Standard C simply states "that access to the object be as fast as possible." (Standard C++ states that register is a "hint to the implementation that the object so declared will be heavily used.") In practice, characters and integers are still stored in registers in the CPU. Larger objects like arrays obviously cannot be stored in a register, but they may still receive preferential treatment by the compiler. Depending upon the implementation of the C/C++ compiler and its operating environment, register variables may be handled in any way deemed fit by the compiler's implementor.
Important points about
register storage class
(1)In following
declaration:
register int a;
We are only requesting not
forcing to compiler to store variable a in CPU. Compiler will decide where to
store in the variable a.
(2)A register variable
execute faster than other variables because it is stored in CPU so during the
execution compiler has no extra burden to bring the variable from memory to
CPU.
(3)Since a CPU have limited
number of register so it is programmer responsibility which variable should
declared as register variable i.e. variable which are using many times should
declared as a register variable.
(4) We cannot dereference
register variable since it has not any memory address. For example:
(a)
#include<stdio.h>
int main(){
register int a=10;
int *p;
p=&a;
printf("%u",p);
}
Output: Compilation error
(b)
#include<stdio.h>
int main(){
register int a,b;
scanf("%d%d",&a,&b);
printf("%d
%d",a,b);
}
Output: Compilation error
(5) Default initial value
of register variable is garbage.
(6) Scope and visibility of
register variable is block.
You can only apply the register specifier to local variables and to the formal parameters in a function. Global register variables are not allowed. Here is an example that uses register variables. This function computes the result of Me for integers:
int int_pwr(register int m, register int e) {
register int temp;
temp = 1;
for(; e; e--) temp = temp * m;
return temp;
}
In this example, e, m, and temp are declared as register variables because they are all used within the loop. The fact that register variables are optimized for speed makes them ideal for control of or use in loops. Generally,register variables are used where they will do the most good, which are often places where many references will be made to the same variable. This is important because you can declare any number of variables as being of type register, but not all will receive the same access speed optimization.
Comments
Post a Comment