The register storage class specifier indicates to the compiler that the value of the object should reside in a machine register. The compiler is not required to honor this request. Because of the limited size and number of registers available on most systems, few variables can actually be put in registers. If the compiler does not allocate a machine register for a register object, the object is treated as having the storage class specifier auto. A register storage class specifier indicates that the object, such as a loop control variable, is heavily used and that the programmer hopes to enhance performance by minimizing access time.
An object having the register storage class specifier must be defined within a block or declared as a parameter to a function.
You can initialize any register object except parameters. If you do not initialize an automatic object, its value is indeterminate. If you provide an initial value, the expression representing the initial value can be any valid C or C++ expression. The object is then set to that initial value each time the program block that contains the object's definition is entered.
Objects with the register storage class specifier have automatic storage duration. Each time a block is entered, storage for register objects defined in that block is made available. When the block is exited, the objects are no longer available for use.
If a register object is defined within a function that is recursively invoked, memory is allocated for the variable at each invocation of the block.
Since a register object is treated as the equivalent to an object of the auto storage class, it has no linkage.
Restrictions
Restrictions
register int i; int* b = &i; // valid in C++, but not in C
Related References