The register storage class specifier indicates to the compiler that the object should be stored in a machine register. The register storage class specifier is typically specified for heavily used variables, such as a loop control variable, in the hopes of enhancing performance by minimizing access time. However, 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.
An object having the register storage class specifier must be defined within a block or declared as a parameter to a function.
The following restrictions apply to the register storage class specifier:
You cannot use pointers to reference objects
that have the register storage class specifier.
You cannot use the register storage
class specifier when declaring objects in global scope.
A register does not have an address. Therefore,
you cannot apply the address operator (&) to a register variable.
You cannot use the register storage
class specifier when declaring objects in namespace scope.Unlike C, C++ lets you take the address of an object with the register storage class. For example:
register int i; int* b = &i; // valid in C++, but not in C
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.
Related information