EmbedwrightArticlesLesson guideFree lessonsThe course · $79

Ring buffer capacity: full and empty are not the same

By Chinmay Gupta · Updated 2026-12-01
Short answer. A ring using only equal read and write indices for empty needs a separate full representation. Reserving one slot is a simple approach: an N-slot buffer stores at most N-1 items.

A circular buffer has a physical array and a logical sequence of unread values. When the write position reaches the end, it returns to the beginning. The physical location is not sufficient to determine occupancy: equal indices can mean no items remain or that the producer has used every position.

The contract

One design deliberately leaves a slot unused. Equal indices then denote empty and the next write position matching the read index denotes full. This costs one storage element and avoids a separate count. Another design keeps a count or more information about the positions. The choices have different invariants and concurrency implications, so document the chosen one.

A small executable example

#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
enum { N = 5 };
static unsigned next(unsigned i) { return (i + 1u) % N; }
static bool full(unsigned w, unsigned r) { return next(w) == r; }
int main(void) {
    unsigned r = 0, w = 0;
    for (unsigned i = 0; i < N - 1; ++i) {
        assert(!full(w, r)); w = next(w);
    }
    assert(full(w, r));
    r = next(r); assert(!full(w, r));
    w = next(w); assert(w == 0 && full(w, r));
    puts("capacity 4, wrap verified");
}
Real output
capacity 4, wrap verified

Compiled with -Wall -Wextra -Werror and run under AddressSanitizer + UBSan.

Learning this for work or an interview? Lessons 1 and 2 of DSA for Firmware are free, with labs and C exercises. Start Lesson 1

What the example does not establish

The model tests only indices with valid initial positions. It does not implement data publication or concurrent access. In particular, an interrupt and foreground loop cannot be assumed to share these positions safely just because the full predicate is correct. Their execution contexts, access widths and synchronization must be addressed separately. Overflow behavior should also be explicit: rejecting the newest item is not equivalent to overwriting the oldest.

Checks to add in your own project

Test the sequence empty, one item, full, one removal, then another insertion across the physical end. A useful reference is a simple FIFO sequence model for comparison with every push and pop. Include rejected operations and verify they do not change the logical contents. Size the buffer from measured bursts and service latency, not merely from an average arrival rate.

The course goes further on this topic. Lesson 10 of DSA for Firmware, “The UART that drops bytes”, adds interactive labs and C exercises with tests that you run on your own machine. Read the lesson guide · See the course
Want the full path from “I know C” to “I can choose the right data structure under a RAM budget”?
DSA for Firmware teaches it one real bug at a time.
  • 28 lessons, each starting from a real firmware bug.
  • 110 C exercises with tests, and a reference solution for every one.
  • Lessons 1 and 2 are free. No sign-up.
Start Lesson 1 freeSee the full course · $79

Get the next article by email

One short email when a new article or free lesson is published. No spam. Unsubscribe at any time by replying to any email. See the privacy policy.

Chinmay Gupta
Chinmay Gupta is a firmware engineer with more than eight years of experience writing and debugging embedded C. He builds Embedwright's DSA for Firmware course.