Objects and memory
Distinguish an object, its value, its representation, and its physical implementation before reasoning about addresses.
In this chapter9 sections
C programs work with objects that occupy storage. We will connect an object to its bytes, size, and address without assuming every machine arranges memory in the same way. The diagrams show relationships you can reason about.
1. An object is storage with a lifetime
A C object is a region of data storage whose contents can represent values. In int count = 4;, count names an object, int supplies its type, and 4 is its initial value. Changing the value does not create a new named object. A function is not a C object, even though implementations also store executable instructions somewhere.
Think of a labeled container. The label helps you refer to it; the container has room and a permitted interpretation; its contents can change. The limit of this analogy is that C objects need not have names, and an optimizer need not give each source-level object a separate physical container. The language describes the behavior a valid program can observe.
2. A C byte is a unit, not a universal picture
<stddef.h> provides size_t for the following fragment. Storage sizes are measured in C bytes. sizeof(char) is always 1; CHAR_BIT from <limits.h> says how many bits one C byte has and is at least eight. Eight-bit bytes are common, but they are not the definition. sizeof(int) is not universally four, and sizeof produces a value of type size_t.
int count = 4;
size_t object_bytes = sizeof count;
size_t character_bytes = sizeof(char);
The program asks its implementation for the sizes instead of writing an assumed byte count. A size is not a count of decimal digits, nor a promise about every value a type can represent. Types also have ranges, alignment requirements, and rules for operations. Capacity and meaning belong together.
3. Copying a value creates independent contents
int original = 4;
int copy = original;
copy = 9;
These two objects initially hold equal integer values. Assigning 9 to copy leaves original holding 4. The second name is not an alternative spelling for the first object. Later, pointers will let two expressions designate the same object; that is a different relationship from copying an integer.
4. A representation is not the value itself
An object representation is the sequence of bytes used to represent an object. A type gives those bytes meaning. Some types can contain padding or representations that must not be read as ordinary values. Do not assume equal values require identical bytes, or that arbitrary bytes make a valid value of every type.
Optional: a byte view to revisit after pointers
unsigned int value = 1;
const unsigned char *bytes = (const unsigned char *)&value;
unsigned char first_byte = bytes[0];
The character-type access rule allows inspecting an object’s representation through unsigned char. This first-byte example deliberately promises no numeric result: byte order and representation belong to the implementation. The cast does not change value into a different declared type. Casting a pointer to an unrelated type does not generally make an otherwise invalid access legal.
5. Addresses describe relationships you may use
The address-of operator, in ordinary cases such as &count, produces a pointer to the object. A pointer is not specified as an ordinary integer holding a physical RAM number. Common hosted operating systems use virtual addresses; C itself does not require that operating-system model. You can reason about “points to this object” without guessing its numeric address.
If you print an object pointer with printf, %p expects a void * argument, for example printf("%p", (void *)&count). The displayed form is implementation-defined. A particular run’s address is a debugging observation, not a portable fact to memorize.
6. Stack and heap are implementation models
Many systems organize code, static data, call frames, and allocated storage into familiar regions. Stack and heap diagrams help explain those systems. They do not define C storage duration. An automatic object might be held in a register or optimized away, and an allocator may obtain storage using several mechanisms. A diagram should describe its assumptions rather than prescribe one layout for every program.
- Object
- Type and representation
- Valid access
- Observable value
Primary references and further reading
- WG14 N1570 — public C11 committee draft: 3.6, 6.2.4–6.2.8, 6.5p7, 6.5.3.4, 7.21.6.1. These stable rules support the C17 baseline; this draft is not the final ISO publication.
Before you move on
- Objects occupy storage and have types, representations, and lifetimes.
- sizeof measures C bytes; CHAR_BIT describes how many bits one C byte contains.
- A conceptual memory diagram explains relationships without fixing one physical machine layout.
Keep these ideas separate
- Object value / Object bytes
- A character view can inspect representation bytes without making every byte pattern a valid value of every type.
Which facts about an object are guaranteed by C, and which details would you check on the target implementation?
Optional: say it aloud or keep a note. You can continue whenever you are ready.Check your understanding
Optional. Answers are not saved or sent, and reading stays unrestricted.
Read the explanation for every choice
- Yes, a fixed stack layout is part of every C object’s type.
A type does not prescribe a universal machine stack layout.
- No. Storage-duration and lifetime rules do not mandate that physical arrangement.
✓ Correct reasoning. A call stack is a useful common implementation model, with limits that should be stated.
- No local object can be optimized.
Implementations can optimize while preserving required program behavior.
C17
Quick reference from the earlier edition
Retained for existing section links and saved passages. Read the expanded explanation above for the connected learning sequence.
The table is a common model for conventional hosted systems, not a layout required by C. Embedded/freestanding implementations, linker choices, and optimizations can use other arrangements. Automatic storage duration does not itself guarantee a physical stack allocation.
| Region | Contents |
|---|---|
| Text (code) segment | Compiled machine instructions, typically read-only |
| Initialized data segment | Typically writable static-storage objects with nonzero initialization; exact placement is implementation-specific |
| BSS segment | Typically zero-initialized static-storage objects, including some with explicit zero initializers |
| Heap | Memory from malloc/calloc/realloc; an allocation can reuse released storage, so the memory region need not grow on every request |
| Stack | Commonly call bookkeeping and some automatic objects; objects may instead be optimized away or held in registers |
A static local object retains its value because its lifetime is the entire execution, regardless of which segment holds it. Deep recursion or large automatic arrays can exhaust limited automatic-storage resources on typical systems. A leak retains allocated storage that can no longer be usefully released; actual heap growth and layout depend on the allocator and environment.
Ready for the next idea?
Mark this chapter as read whenever you feel ready.