1. Gyanastra
  2. Data Structures
  3. Storage Classes
  4. Register Storage Class and Address-of Restriction
Free lesson, no account needed 7 min read 8 practice questions after it
Asked in past papers5 times
  • GATE CSE20172012
  • UGC NET201420132009

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, and static.
  • register storage 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 register variable is therefore asking for the address of a register, which some compilers cannot produce.
  • register pointer: a pointer variable whose own storage class is register. It can hold the address of an ordinary variable without any problem.

How it works

  1. Taking the address of a register variable. If the compiler follows the advice, it stores the variable (say i) in a register rather than in the activation record. Writing a = &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.
  2. A register pointer holding the address of an ordinary variable.
    • int i; creates i inside the activation record of main, which is created in the stack; being automatic (local), i has some address, say 100.
    • register int *a; declares a pointer to int whose storage class is register, requesting that a be kept in a register and not in the activation record.
      The compiler may or may not take the advice; otherwise a is stored in the activation record of main.
    • a = &i; stores 100 in a — either in the register or in the activation record. Either way a has storage and can hold the value.
    • printf("%d", *a); goes to the address present in a and prints the value there; if the value at address 100 is 1, then 1 is 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.
  3. Conflicting storage class specifiers. In register static int i = 10;, static explicitly asks for storage in the data section while register asks 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 i in a register, it cannot give the address of a register → error or warning (compiler dependent).
  • If the compiler ignored the advice and kept i in 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);
}
StepStatementMemory / register stateOutput
1int i;i created in activation record of main on the stack; address of i = 100; value at 100 = 1—
2register int *a;Request to compiler: keep a in a register (compiler may instead keep it in the activation record of main)—
3a = &i;a holds 100 (either in the register or in the activation record)—
4printf("%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

SituationWhat is requestedResult
register int i; then &iAddress of a variable the compiler may keep in a registerCompiler 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 pointerNo 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, and static are storage classes; register requests storage in a CPU register rather than in the activation record.
  • The compiler may or may not honour a register request — it is only advice.
  • You cannot (portably) take the address of a register variable: registers generally do not have addresses.
    Behaviour is compiler dependent — error in some compilers, warning in others.
  • A register pointer 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 static and register on 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
  • static variables (both local and global) are stored in the data segment, not on the stack; a local static keeps its value between function calls while remaining invisible outside its block.
    Asked inGATE 2017UGC NET 2014
  • register is only a hint: the compiler may ignore it, and the address-of operator & cannot be applied to a register variable (a constraint violation in standard C, so a conforming compiler must diagnose it).
    Asked inGATE 2017
  • auto may be used only for block-scope (local) variables; using it at file scope is an error. extern declares without defining and gives the variable global scope/linkage.
    Asked inUGC NET 2009UGC NET 2014
  • union is a derived/user-defined type keyword, not a storage class; likewise typedef is 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 *a is a register-stored pointer to an ordinary int, and &i on a non-register i is always legal.
    Asked inGATE 2017
Pages 1–2 of 2