liveBook · Manning

Register keyword

As explained in Modern C

The register keyword is a C storage-class specifier that requests register storage and restricts use of the object's address in applicable contexts.

The register keyword declares a block-scope variable with automatic storage duration while forbidding use of the & operator on that variable. The restriction lets the compiler see that the variable cannot be reached through an address. section 13.2.2

A local register variable

void work(void) { register int counter; counter = 0; } The declaration keeps counter within the function's automatic-storage rules. Writing &counter is not allowed, so counter cannot alias another object through a pointer. section 13.2.2

TAKEAWAY

  • A register variable is created when execution enters its defining block and destroyed when execution leaves it, except where variable-length arrays or temporary objects change the case. section 13.2.2
  • Each recursive call creates a separate local instance of an automatic register variable. section 13.2.2
  • Declaring suitable local variables as register can expose address-taking sites and may help optimization by inhibiting aliasing. section 13.2.2 section 15.0

Arrays require separate care because they decay to the address of their first element in almost all contexts. The register technique therefore works best for non-array variables that contain no arrays. section 13.2.2

How Modern C covers it

How deep it goes
Explains automatic lifetime, recursion, address prohibition, aliasing, and array limitations, but does not develop compiler implementation details.
Examples
Uses block-scope locals, recursive calls, address-taking, non-array variables, and arrays as contrasting cases.
What it assumes
Assumes block scope, pointers, addresses, and automatic storage, while defining their relationship to register variables.
What you can do after
Apply it via C declarations to diagnose unintended address-taking and communicate potential aliasing constraints to the compiler.
How it connects
Connects automatic storage duration, recursion, pointers, arrays, aliasing, and compiler optimization.

Also explained in

Compare how 2 books explain register keyword →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.