liveBook · Manning

Chapter 13: Storage

What you will learn in chapter 13 of Modern C, Third Edition

The introduction contrasts the lifetimes of previously encountered objects with storage instances created by recursive calls and dynamic allocation.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction contrasts the lifetimes of previously encountered objects with storage instances created by recursive calls and dynamic allocation.

Key terms: object lifetime, identifier visibility, compound literals, static, storage instance, recursive function, object, dynamic allocation, storage instances, byte arrays

13.1 malloc and friends

Introduces dynamic allocation in C, the allocation functions, string-duplication helpers, and the rules for sizing, checking, and initializing allocated storage.

Key terms: dynamic allocation, malloc, free, calloc, realloc, aligned_alloc, null pointer, failed allocations, strdup, strndup

13.1.1 A complete example with varying array size

A dynamically allocated circular buffer example shows how to manage its storage, resize it while preserving its contents, and guide callers toward safe use of its interface.

Key terms: tab, cap, start, len, circular buffer, malloc, free, null pointer, realloc, [[nodiscard]]

13.1.2 Ensuring consistency of dynamic allocations

This subsection explains how to keep dynamic allocations consistent by matching allocations with frees and passing only valid pointers to free.

Key terms: allocation, free, memory leak, realloc, malloc, calloc, aligned_alloc, null

13.1.3 Flexible array members

This section introduces flexible array members as a way to keep an array and its length in one structure, while explaining the storage and consistency requirements they leave to the programmer.

Key terms: flexible array member, incomplete array, length, storage, length member, consistency

13.2 Storage duration, lifetime, and visibility

This section distinguishes an identifier’s visibility from an object’s identity and lifetime, then introduces storage durations and their relationship to linkage and compound literals.

Key terms: visibility scope, identifier, shadowed, object, definition, declaration, extern, distinct object, read-only object literals, string literals

13.2.1 Static storage duration

This section identifies objects with static storage duration and explains their program-long lifetime, initialization, and visibility.

Key terms: static storage duration, file scope, static, thread_local, string literals, lifetime, initialization, process startup procedure, linker, visible

13.2.2 Automatic storage duration

This section explains the lifetimes of ordinary automatic and temporary objects, and how register declarations affect address-taking and aliasing.

Key terms: automatic storage duration, lifetime, block, recursive call, register, & operator, alias, arrays, temporary lifetime, function return value

13.3 Digression: Using objects before their definition

This section explains why an ordinary automatic object can be used before execution reaches its definition, how repeated evaluation affects its value, and why VLAs follow a different lifetime rule.

Key terms: lifetime, scope of definition, automatic objects, compound literal, initializer, p, q, VLA, visibility scope, runtime property

13.4 Initialization

This section explains how storage duration affects initialization and how consistent initialization functions can keep allocated objects in a well-defined state.

Key terms: static storage duration, thread-storage duration, automatic storage duration, allocated storage duration, default initialization, initializers, calloc, malloc, assignment, VLAs

13.5 Digression: A machine model

This optional digression uses assembly for one machine to illustrate how automatic storage is set up, how optimization can change the resulting code, and why VLAs need runtime stack adjustments.

Key terms: von Neumann model, assembler, registers, instructions, automatic objects, stack, %rbp, %rsp, initialization, optimization

13.6 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 12: The C memory model Chapter 14: More involved processing and I/O →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.