liveBook · Manning

Object

As explained in Modern C

A data object is a C entity that occupies storage for a value.

A value needs a place

Consider `size_t i = 0;`. The identifier `i` names a variable, its type specifies what kind of value it holds, and the initializer gives it the value 0. The object is the storage holding that value—not the name, the type, or the value itself. section 2.3 section 2.2

A declaration specifies an identifier and its properties; a definition establishes the object it refers to. In these introductory examples, a declaration with an initializer does both. TAKEAWAY: Declaring a name and defining an object are different jobs. Functions can also be defined, but they are not data objects. section 2.3 section 2.5

 8   double A[5] = {
 9     [0] = 9.0,
10     [1] = 2.9,
11     [4] = 3.E+25,
12     [3] = .00007,
13   };


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":1},{\"line\":0,\"ch\":2}],[{\"line\":0,\"ch\":5},{\"line\":0,\"ch\":11}],[{\"line\":1,\"ch\":1},{\"line\":1,\"ch\":2}],[{\"line\":2,\"ch\":15},{\"line\":2,\"ch\":16}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":2}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":2}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":2}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":2}]]"}
!@%STYLE%@!

The designated initializer defines a five-element array object. Its elements receive values by index; the omitted element at index 2 becomes 0.0. The first index is 0, not 1. section 2.3

An object has a type—a fixed interpretation—and a value that can vary during execution. Usually, reason about that value rather than its physical representation. When representation matters, C models an object as a collection of bytes; an address obtained with `&` provides pointer access to an object, subject to the stated exception for `register` objects. A pointer does not tell you where the object physically resides. section 5.0 section 12.0

Listing 13.2. An example of shadowing with an extern variable
 1   #include <stdio.h>
 2
 3   unsigned i = 1;
 4
 5   int main(void) {
 6     unsigned i = 2;        /* A new object */ 
 7     if (i) {
 8       extern unsigned i;   /* An existing object */ 
 9       printf("%u\n", i);
10     } else {
11       printf("%u\n", i);
12     }
13   }


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

The file-scope `i` and the local `i` are distinct objects. The inner `extern` declaration refers back to the file-scope object rather than defining a third one, so the program prints 1. TAKEAWAY: A name’s visibility and an object’s identity are not the same thing. section 13.2

Every variable definition creates a distinct object. Likewise, each function call defines a new instance of its local objects, so simultaneously active recursive calls have separate local state. Pointers can still let those calls modify shared data outside those local instances. section 13.2 section 17.4

How Modern C covers it

How deep it goes
Defines objects through initialization and storage, then relates identity, local instances, addresses, and byte representation without specifying physical placement.
Examples
Uses a zero-initialized variable, a designated array initializer, and shadowed variables with an `extern` declaration.
What it assumes
Introduces declarations, types, and identifiers from basic C examples before discussing pointers and the memory model.
What you can do after
Diagnose/debug with it by telling distinct objects apart even when names are shadowed or recursive calls use the same local declaration.
How it connects
Links objects to declarations, definitions, types, values, arrays, scope, pointers, and storage duration.

Also explained in

Compare how 4 books explain 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.