liveBook · Manning

Circular buffer

As explained in Object-Oriented Software Design in C++

A fixed-capacity queue implemented with storage whose insertion and removal positions wrap around from the end to the beginning.

A circular buffer is a fixed-capacity queue backed by an array. It tracks the head, tail, and current count; insertion and removal advance indexes that wrap from the array’s end to its beginning. section 6.3.1

The core operations follow a simple contract:

  • add(value) inserts at the tail, advances the tail, and requires that the buffer is not full. section 6.3.1 section 6.3.2
  • remove() returns the value at the head, advances the head, and requires that the buffer is not empty. section 6.3.1 section 6.3.2
  • Programming by contract expresses these requirements with preconditions, postconditions, and a class invariant checking valid head and tail indexes. section 6.3 section 6.3.3

Filling, draining, and detecting misuse

A capacity-five buffer accepts 10, 20, 30, 40, and 50, then removes them in the same order. Calling remove() once more violates its precondition and triggers an assertion failure; callers should check get_count() first.

How Object-Oriented Software Design in C++ covers it

How deep it goes
Explains storage, indexes, wrapping, and contract checks, but stops short of alternative overflow policies.
Examples
Uses a capacity-five integer buffer, sequential additions and removals, tester guards, and an assertion after empty removal.
What it assumes
Assumes basic C++ classes, arrays, member functions, and assertions while teaching the buffer’s contract explicitly.
What you can do after
Build it from scratch, operate it through add and remove, and diagnose invalid calls with contract assertions.
How it connects
Connects circular buffers to programming by contract, preconditions, postconditions, class invariants, and assertions.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Object-Oriented Software Design in C++. The section links point to the parts of the book it draws on.