liveBook · Manning

Automatic object

As explained in Modern C, Third Edition

An automatic variable is a C object with automatic storage duration, normally created on entry to a block and destroyed on exit from it.

In `void f(void) { unsigned n = 0; { unsigned x = n + 1; } }`, `x` is an automatic object. Its lifetime belongs to the execution of the inner block: it begins when execution enters that block and ends when execution leaves. The initializer supplies its value; it does not start its lifetime. section 13.2.2 section 13.3

TAKEAWAY — Storage duration answers how long an object exists, not whether it has been initialized.

  • Block-scope variables and compound literals without `static` generally have automatic storage duration. Ordinary automatic objects live for one execution of their defining block; variable-length arrays and temporary objects need separate rules. section 13.2.2
  • Each recursive call creates a separate local instance. Do not confuse repeated execution within one block with a recursive call that enters a new instance of the block. section 13.2.2 section 13.3
  • The address-of operator `&` cannot be applied to an object declared `register`. This prevents taking its address and gives the compiler useful information about aliasing. section 13.2.2

Jumping past a compound literal

In `fgoto`, `goto` returns execution to a label without leaving the function’s block. Each evaluation of `&((unsigned){ j, })` in that block refers to the same compound-literal object, while its value is set again. For `fgoto(2)`, the second printed line therefore reports that `p` and `q` are equal and `*p` is `1`; the pointers remain valid. The example makes the distinction between an object’s lifetime and repeated evaluation of its initializer observable. section 13.3

Listing 13.3 A contrived example for the use of a compound literal
void fgoto(unsigned n) {
  unsigned j = 0;
  unsigned* p = nullptr;
  unsigned* q;
 AGAIN:
  if (p) printf("%u: p and q are %s, *p is %u\n",
                j,
                (q == p) ? "equal" : "unequal",
                *p);
  q = p;    1 
  p = &((unsigned){ j, }); 
  ++j;
  if (j <= n) goto AGAIN;
}

The `goto` example exposes how repeated evaluations can update one automatic compound literal without creating a new object on each iteration. section 13.3

On one illustrated machine, a function reserves stack space for its automatic objects on entry, then initializes selected objects with later instructions. That layout helps explain why reserved space need not already hold initialized values, but it is a machine-model illustration, not a required physical representation: an optimized version can use a different layout. In threaded code, prefer function arguments and automatic variables for data a thread uses locally. section 13.5 section 20.8

How Modern C, Third Edition covers it

How deep it goes
Works through block-lifetime rules and a compound-literal jump example, then illustrates stack setup without making it a language requirement.
Examples
Uses a `goto` loop, its printed pointer comparisons, and unoptimized and optimized assembler listings.
What it assumes
Assumes familiarity with C declarations, blocks, and pointers; introduces `goto` and labels for the lifetime example.
What you can do after
Diagnose/debug with it whether local objects and pointers remain valid across jumps, repeated evaluations, and recursive calls.
How it connects
Connects automatic storage duration to block scope, compound literals, recursion, aliasing, stack frames, and thread-local work.

Also explained in

Compare how 2 books explain automatic object →

Related concepts

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