liveBook · Manning

Circular buffer

As explained in Modern C

A circular buffer is a bounded FIFO data structure whose storage is treated as wrapping from the end back to the beginning.

A circular buffer is a bounded FIFO structure for double values. Append adds at the rear; pop removes the oldest value from the front. Storage wraps around, allowing positions to reuse a dynamically allocated array without exceeding its maximum length. section 13.1.1

TAKEAWAY: Treat the type through its interface and pair each allocation strategy with its matching cleanup.

  • Use circular_init and circular_destroy with an existing circular object. section 13.1.1
  • Use circular_new and circular_delete when the buffer itself is allocated dynamically. section 13.1.1
  • Inspect positions with circular_element and query the stored count with circular_getlength; resize only when the new capacity can retain the current elements. section 13.1.1
typedef struct circular circular;
!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":14}]]"}
!@%STYLE%@!

The opaque typedef exposes a usable circular type while hiding its representation. section 13.1.1

How Modern C covers it

How deep it goes
Explains the interface, lifecycle, hidden fields, wraparound indexing, and resizing mechanics, but does not show a complete client program.
Examples
Uses double-valued append, pop, positional access, initialization, destruction, allocation, deletion, and resizing operations.
What it assumes
Assumes basic C types and functions while demonstrating opaque structures, malloc, realloc, free, and object lifecycle from context.
What you can do after
Operate a C circular-buffer interface and reason about capacity, element positions, allocation failure, and matching cleanup.
How it connects
Connects circular buffers with FIFO behavior, opaque types, dynamic memory allocation, arrays, and resizing.

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.