How It Works
A circular buffer first starts empty and of some predefined length. For example, this is a 7-element buffer:
Assume that a 1 is written into the middle of the buffer (exact starting location does not matter in a circular buffer):
Then assume that two more elements are added — 2 & 3 — which get appended after the 1:
If two elements are then removed from the buffer, the oldest values inside the buffer are removed. The two elements removed, in this case, are 1 & 2 leaving the buffer with just a 3:
If the buffer has 7 elements then it is completely full:
A consequence of the circular buffer is that when it is full and a subsequent write is performed, then it starts overwriting the oldest data. In this case, two more elements — A & B — are added and they overwrite the 3 & 4:
Alternatively, the routines that manage the buffer could prevent overwriting the data and return an error or raise an exception. Whether or not data is overwritten is up to the semantics of the buffer routines or the application using the circular buffer.
Finally, if two elements are now removed then what would be returned is not 3 & 4 but 5 & 6 because A & B overwrote the 3 & the 4 yielding the buffer with:
Read more about this topic: Circular Buffer
Famous quotes containing the word works:
“We all agree nowby we I mean intelligent people under sixtythat a work of art is like a rose. A rose is not beautiful because it is like something else. Neither is a work of art. Roses and works of art are beautiful in themselves. Unluckily, the matter does not end there: a rose is the visible result of an infinitude of complicated goings on in the bosom of the earth and in the air above, and similarly a work of art is the product of strange activities in the human mind.”
—Clive Bell (18811962)