Data Structures · Storage Classes
Register Storage Class and Address-of Restriction
Overview
The register storage class asks the compiler to keep a variable in a CPU register instead of in the activation record (stack/memory).
Because registers generally have no addresses, taking the address of a register variable is restricted, and combining register with another storage class such as static is contradictory.
Key concepts
- Storage class: a keyword applied while defining a variable that tells the compiler where to store the variable (which memory area) and how it behaves. The storage classes named here are
register,extern, andstatic. registerstorage class: a request/advice to the compiler to store the variable in a register instead of in the activation record (stack/memory). The compiler may or may not honour it.- Address-of restriction: memory elements generally have addresses, but registers might not. Asking for the address of a
registervariable is therefore asking for the address of a register, which some compilers cannot produce. registerpointer: a pointer variable whose own storage class isregister. It can hold the address of an ordinary variable without any problem.
How it works
- Taking the address of a
registervariable. If the compiler follows the advice, it stores the variable (sayi) in a register rather than in the activation record. Writinga = &i;then asks for the address of a register.
Some compilers give an error and some give a warning; the behaviour is compiler dependent, and such a program is not supposed to be written. - A
registerpointer holding the address of an ordinary variable.int i;createsiinside the activation record ofmain, which is created in the stack; being automatic (local),ihas some address, say100.register int *a;declares a pointer tointwhose storage class isregister, requesting thatabe kept in a register and not in the activation record.
The compiler may or may not take the advice; otherwiseais stored in the activation record ofmain.a = &i;stores100ina— either in the register or in the activation record. Either wayahas storage and can hold the value.printf("%d", *a);goes to the address present inaand prints the value there; if the value at address100is1, then1is printed.- No problem arises here because the address of the variable to be stored in the register is not being requested; the address of another variable is stored inside a variable that is either in the register or in the activation record.
- Conflicting storage class specifiers. In
register static int i = 10;,staticexplicitly asks for storage in the data section whileregisterasks for storage in a register — two places at once, which cannot be said. This gives a compiler error.
Formulas
- No formulas are given in this section.
Worked examples
Example 1 — Address of a register variable
int main()
{
register int i;
int *a;
a = &i; /* asking for the address of a register */
}
- If the compiler stored
iin a register, it cannot give the address of a register → error or warning (compiler dependent). - If the compiler ignored the advice and kept
iin the activation record, the address exists and the program may compile and run. - Conclusion: compiler dependent; not supposed to write programs like this.
Example 2 — register pointer to an ordinary variable
int main()
{
int i; /* address of i = 100, value at 100 is 1 */
register int *a;
a = &i;
printf("%d", *a);
}
| Step | Statement | Memory / register state | Output |
|---|---|---|---|
| 1 | int i; | i created in activation record of main on the stack; address of i = 100; value at 100 = 1 | — |
| 2 | register int *a; | Request to compiler: keep a in a register (compiler may instead keep it in the activation record of main) | — |
| 3 | a = &i; | a holds 100 (either in the register or in the activation record) | — |
| 4 | printf("%d", *a); | Go to address stored in a (100), read the value there (1) | 1 |
Example 3 — Conflicting storage classes
int main()
{
int i = 10;
register static int i = 10;
}
static→ store in data section.register→ store in register.- Two contradictory locations requested → compiler error.
Comparison
| Situation | What is requested | Result |
|---|---|---|
register int i; then &i | Address of a variable the compiler may keep in a register | Compiler dependent: some compilers give an error, some give a warning; not supposed to be written |
int i; register int *a; a = &i; | Address of an ordinary (stack) variable stored inside a register pointer | No problem; works whether a is kept in a register or in the activation record |
register static int i; | Two storage locations at once (data section by static, register by register) | Compiler error |
Revision summary
register,extern, andstaticare storage classes;registerrequests storage in a CPU register rather than in the activation record.- The compiler may or may not honour a
registerrequest — it is only advice. - You cannot (portably) take the address of a
registervariable: registers generally do not have addresses.
Behaviour is compiler dependent — error in some compilers, warning in others. - A
registerpointer holding the address of an ordinary variable is perfectly fine: you are not asking for the address of the register variable itself. - Combining two storage class specifiers such as
staticandregisteron the same variable is contradictory (data section vs register) and gives a compiler error. - Shortcut:
register= "keep it in a register" → no&allowed on it;register+static= two homes requested → error.
Previous-year exam insights
- The four storage classes and their properties:
auto(local, stack, default for block variables, scope local, life = block),register(request for CPU register, scope local, life = block),static(local static: data section, scope local, life = whole program, retains value between calls;
global static: file scope),extern(declaration only, global scope, defined elsewhere).
Asked inUGC NET 2014UGC NET 2009GATE 2017 staticvariables (both local and global) are stored in the data segment, not on the stack; a localstatickeeps its value between function calls while remaining invisible outside its block.
Asked inGATE 2017UGC NET 2014registeris only a hint: the compiler may ignore it, and the address-of operator&cannot be applied to aregistervariable (a constraint violation in standard C, so a conforming compiler must diagnose it).
Asked inGATE 2017automay be used only for block-scope (local) variables; using it at file scope is an error.externdeclares without defining and gives the variable global scope/linkage.
Asked inUGC NET 2009UGC NET 2014unionis a derived/user-defined type keyword, not a storage class; likewisetypedefis a storage-class specifier syntactically but does not allocate storage.
Asked inUGC NET 2009- A pointer variable's storage class is independent of the type it points to:
register int *ais a register-stored pointer to an ordinary int, and&ion a non-registeriis always legal.
Asked inGATE 2017