liveBook · Manning

Allocated storage duration

As explained in Modern C

Allocated storage is a region of C storage obtained dynamically for use in representing objects during execution.

Allocating and initializing an object

rat const* myRat = rat_init(malloc(sizeof(rat)), 13, 7);\n\nHere, malloc obtains storage during execution, and rat_init explicitly puts that storage into a usable state. The storage remains available until it is released with free. section 13.4 section 13.1

Allocated storage duration is the C storage duration created explicitly by calls such as malloc and free. It supports collections whose size is known only during execution, including growing input, irregular data, large matrices, and streams. section 13.1 section 13.2

TAKEAWAY: An allocated object has a lifetime with a starting point and an endpoint. It cannot be accessed outside that lifetime; doing so has undefined behavior. Its lifetime is controlled explicitly rather than automatically determined by a declaration or function execution. section 13.2

The standard allocation interface separates several related operations:

  • malloc obtains storage measured in bytes; sizeof *pointer keeps the requested size aligned with the pointer's type. section 13.1
  • free releases storage that is no longer needed. section 13.1
  • calloc provides a specialized allocation whose new storage has all bits set to 0; realloc grows or shrinks storage; aligned_alloc ensures nondefault alignment. section 13.1
  • Allocated storage is not automatically initialized, so an object or array placed there needs explicit initialization, often through a type-specific _init function. section 13.4
Table 13.1. Storage classes, scope, linkage of identifiers, and storage duration of the associated objects Tentative indicates that a definition is implied only if there is no other definition with an initializer. Induced indicates that the linkage is internal if another declaration with internal linkage has been met prior to that declaration; otherwise, it is external. (view table figure)
Class Scope Definition Linkage Duration Lifetime
Initialized File Yes External Static Whole execution
extern, initialized File Yes External Static Whole execution
Compound literal File Yes N/A Static Whole execution
String literal Any Yes N/A Static Whole execution
static, initialized Any Yes Internal Static Whole execution
Uninitialized File Tentative External Static Whole execution
extern, uninitialized Any No Induced Static Whole execution
static, uninitialized Any Tentative Internal Static Whole execution
thread_local File Yes External Thread Whole thread
extern thread_local Any No External Thread Whole thread
static thread_local Any Yes internal Thread Whole thread
Compound literal     N/A    
Non-VLA     None    
Non-VLA, auto Block Yes None Automatic Block of definition
register     None    
VLA Block Yes None Automatic From definition to end of block
Function return with array Block Yes None Automatic To the end of expression

Compare allocated duration with static, automatic, and thread storage durations, alongside scope, linkage, initialization, and lifetime. section 13.2

How Modern C covers it

How deep it goes
Defines allocated duration, allocation functions, lifetime, and initialization, but defers broader declaration and linkage details to comparison material.
Examples
Uses a dynamically allocated rational number, a vector of doubles, and allocation helpers for initialized rational arrays.
What it assumes
Assumes familiarity with C objects and pointers while defining storage duration, lifetime, and explicit initialization in context.
What you can do after
Apply it via malloc-family functions, explicit initialization, and free while reasoning about object lifetime.
How it connects
Connects dynamic allocation with malloc, free, object lifetime, explicit initialization, scope, linkage, and other storage durations.

Also explained in

Compare how 2 books explain allocated storage duration →

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.