liveBook · Manning

Automatic object

As explained in Modern C

An automatic variable is a C variable whose storage is created on entry to its enclosing block and released when execution leaves that block.

In a function, a local variable such as `unsigned j = 0;` is ordinarily an automatic object. Its lifetime begins when execution enters its defining block and ends when execution leaves that block. Initialization is a separate event: entering the block does not itself initialize every automatic variable. section 13.2.2 section 13.3 section 13.5

Block-scope variables not declared `static` ordinarily have automatic storage duration; `auto` is the default, and `register` also declares an automatic variable. Block-scope compound literals and some temporary objects returned by function calls are automatic objects too. Variable-length arrays and temporary objects are exceptions to the ordinary block-lifetime rule. section 13.2.2

TAKEAWAY: Each recursive call creates a new local instance of an automatic object. Multiple instances of the same local variable can therefore exist at once. section 13.2.2

Listing 13.3. A contrived example for the use of a compound literal
 3   void fgoto(unsigned n) {
 4     unsigned j = 0;
 5     unsigned* p = 0;
 6     unsigned* q;
 7    AGAIN:
 8     if (p) printf("%u: p and q are %s, *p is %u\n",
 9                   j,
10                   (q == p) ? "equal" : "unequal",
11                   *p);
12     q = p;
13     p = &((unsigned){ j, });
14     ++j;
15     if (j <= n) goto AGAIN;
16   }


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":1},{\"line\":0,\"ch\":2}],[{\"line\":10,\"ch\":1},{\"line\":10,\"ch\":2}],[{\"line\":0,\"ch\":5},{\"line\":0,\"ch\":9}],[{\"line\":0,\"ch\":10},{\"line\":0,\"ch\":15}],[{\"line\":0,\"ch\":16},{\"line\":0,\"ch\":24}],[{\"line\":1,\"ch\":7},{\"line\":1,\"ch\":15}],[{\"line\":2,\"ch\":7},{\"line\":2,\"ch\":15}],[{\"line\":3,\"ch\":7},{\"line\":3,\"ch\":15}],[{\"line\":10,\"ch\":14},{\"line\":10,\"ch\":22}],[{\"line\":1,\"ch\":1},{\"line\":1,\"ch\":2}],[{\"line\":11,\"ch\":1},{\"line\":11,\"ch\":2}],[{\"line\":0,\"ch\":16},{\"line\":0,\"ch\":24}],[{\"line\":1,\"ch\":7},{\"line\":1,\"ch\":15}],[{\"line\":2,\"ch\":7},{\"line\":2,\"ch\":15}],[{\"line\":3,\"ch\":7},{\"line\":3,\"ch\":15}],[{\"line\":10,\"ch\":14},{\"line\":10,\"ch\":22}],[{\"line\":2,\"ch\":1},{\"line\":2,\"ch\":2}],[{\"line\":12,\"ch\":1},{\"line\":12,\"ch\":2}],[{\"line\":0,\"ch\":16},{\"line\":0,\"ch\":24}],[{\"line\":1,\"ch\":7},{\"line\":1,\"ch\":15}],[{\"line\":2,\"ch\":7},{\"line\":2,\"ch\":15}],[{\"line\":3,\"ch\":7},{\"line\":3,\"ch\":15}],[{\"line\":10,\"ch\":14},{\"line\":10,\"ch\":22}],[{\"line\":3,\"ch\":1},{\"line\":3,\"ch\":2}],[{\"line\":13,\"ch\":1},{\"line\":13,\"ch\":2}],[{\"line\":0,\"ch\":16},{\"line\":0,\"ch\":24}],[{\"line\":1,\"ch\":7},{\"line\":1,\"ch\":15}],[{\"line\":2,\"ch\":7},{\"line\":2,\"ch\":15}],[{\"line\":3,\"ch\":7},{\"line\":3,\"ch\":15}],[{\"line\":10,\"ch\":14},{\"line\":10,\"ch\":22}],[{\"line\":4,\"ch\":1},{\"line\":4,\"ch\":2}],[{\"line\":4,\"ch\":6},{\"line\":4,\"ch\":11}],[{\"line\":12,\"ch\":24},{\"line\":12,\"ch\":29}],[{\"line\":5,\"ch\":1},{\"line\":5,\"ch\":2}],[{\"line\":5,\"ch\":7},{\"line\":5,\"ch\":9}],[{\"line\":12,\"ch\":7},{\"line\":12,\"ch\":9}],[{\"line\":5,\"ch\":14},{\"line\":5,\"ch\":20}],[{\"line\":6,\"ch\":1},{\"line\":6,\"ch\":2}],[{\"line\":7,\"ch\":0},{\"line\":7,\"ch\":2}],[{\"line\":8,\"ch\":0},{\"line\":8,\"ch\":2}],[{\"line\":9,\"ch\":0},{\"line\":9,\"ch\":2}],[{\"line\":10,\"ch\":0},{\"line\":10,\"ch\":2}],[{\"line\":0,\"ch\":16},{\"line\":0,\"ch\":24}],[{\"line\":1,\"ch\":7},{\"line\":1,\"ch\":15}],[{\"line\":2,\"ch\":7},{\"line\":2,\"ch\":15}],[{\"line\":3,\"ch\":7},{\"line\":3,\"ch\":15}],[{\"line\":10,\"ch\":14},{\"line\":10,\"ch\":22}],[{\"line\":11,\"ch\":0},{\"line\":11,\"ch\":2}],[{\"line\":12,\"ch\":0},{\"line\":12,\"ch\":2}],[{\"line\":5,\"ch\":7},{\"line\":5,\"ch\":9}],[{\"line\":12,\"ch\":7},{\"line\":12,\"ch\":9}],[{\"line\":0,\"ch\":11},{\"line\":0,\"ch\":15}],[{\"line\":12,\"ch\":19},{\"line\":12,\"ch\":23}],[{\"line\":4,\"ch\":6},{\"line\":4,\"ch\":11}],[{\"line\":12,\"ch\":24},{\"line\":12,\"ch\":29}],[{\"line\":13,\"ch\":0},{\"line\":13,\"ch\":2}]]"}
!@%STYLE%@!

Repeated execution reaches the same block-scope compound literal through `goto`. For `fgoto(2)`, the second printed line reports equal pointers and `*p` equal to 1: the literal denotes the same automatic object across those iterations. section 13.3

The lifetime of an ordinary automatic object starts on block entry, not when execution first reaches its definition. This distinction matters in the compound-literal example: a jump can return to code that uses the object before execution reaches the literal again. section 13.3

A variable declared `register` cannot have its address taken with `&`. This restriction can help reveal address-taking in performance-critical code, but arrays need special care because they decay to the address of their first element in most contexts. section 13.2.2

A machine may reserve space for a function’s automatic objects at entry without initializing that space. This is one implementation model, not a requirement that every automatic object occupy a particular memory location. Automatic variables can also provide local data for thread functions. section 13.5 section 18.7

How Modern C covers it

How deep it goes
Develops block-lifetime rules and a compound-literal example, then illustrates an unoptimized machine setup; distinguishes VLA and temporary-object exceptions.
Examples
Uses a repeated compound literal with goto and compares its behavior with an assembler-level function setup.
What you can do after
Evaluate and understand when ordinary automatic objects exist, and distinguish their lifetime from initialization.
How it connects
Connects automatic storage duration with recursion, compound literals, goto, register declarations, 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. The section links point to the parts of the book it draws on.