liveBook · Manning

Allocated storage duration

As explained in Modern C, Third Edition

Allocated storage duration is the duration of an object created by a dynamic allocation function and ended by deallocation, reallocation, or program termination.

Allocated storage duration begins when a dynamic allocation function creates an object and ends when free or realloc destroys it. Without either call, it lasts until program execution ends. section 13.2

Track allocated objects with these rules:

  • Recognize malloc, calloc, realloc, and aligned_alloc as operations that can create allocated objects. section 13.2
  • End the duration with free or a realloc operation that destroys the object; otherwise, rely on program termination. section 13.2
  • Initialize allocated objects explicitly; use calloc when setting every bit to zero is a valid initialization. section 13.4
  • Encapsulate recurring initialization in a function for each data type. section 13.4

Allocating and initializing a value

rat const* myRat = rat_init(malloc(sizeof(rat)), 13, 7); The malloc call creates storage for a rat object; rat_init then establishes its value explicitly. section 13.4

rat const* myRat = rat_init(malloc(sizeof(rat)), 13, 7);

Inspect the compact allocation-and-initialization expression for a rat object. section 13.4

How Modern C, Third Edition covers it

How deep it goes
Defines lifetime boundaries and initialization obligations, without developing allocator internals.
Examples
Uses malloc with rat_init and recommends calloc or type-specific initialization functions for allocated data.
What you can do after
Operate dynamically allocated objects by selecting an allocation function, initializing storage, and ending its duration correctly.
How it connects
Connects storage duration with dynamic allocation, deallocation, realloc, lifetime, and explicit initialization.

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, Third Edition. The section links point to the parts of the book it draws on.