Do you just get stuck in a never ending line?Well a circular buffer stores data in a fixed size array.
Once the size is set, if more data is added to the buffer while its full, the oldest item in the buffer will be booted out.
By storing two pointers to the front and back of the structure, there is no need to dynamically manipulate the array.
If the buffer is full and you need to add something new, simply set the list item pointed to by the back pointer to the new item and increment the back pointer by one.
The front will now point to the most recently added item.
This technique of memory allocation has benefits when you’re working with a lot of data in real time.
As more data is added to the structure the old data is removed and no data ever needs to be shifted.
As stated before, the pointers just shift positions.
Getting back to your keyboard and password input… All of your keystrokes are held in a circular buffer.
Potentially, if you typed fast enough, you would lose some of the input received from the keystrokes.
A poorly drawn circular buffer restaurant.
The Circular Buffer RestaurantTo help imagine the circular buffer, lets build an automated restaurant.
This restaurant has a single pick-up window (the back of the buffer) where one customer can approach at a time to get their meal (the data).
Behind the window is a rotating, circular table that holds 8 meals (the buffer).
There is a light that points to the front of the table which is just a certain spot on the table (the front of the buffer).
Every time a meal is taken from the window, the table rotates clockwise one spot.
When the chef has finished cooking a meal, he places the meal one spot in front of the place that is lit up, and then the light shines on the newly placed dish.
If the buffer is full and a meal is ready to come off of the stove, the meal at the window will be dumped into the trash.
The table will rotate, and the new meal will be placed in the empty spot.
Use CasesAn instructor of mine informed me that Leet Motion, an augmented reality tech firm that he used to be a part of, used a circular buffer to hold frames of the movements that were input by the user.
They recorded the motion of users hands and body movements and streamed that in real time to a display.
If a user input a lot of movements in a short period of time, the movements would be stored.
When you’re streaming a movie or TV show and you decide to rewind, those previous frames are stored in a circular buffer so that they can be accessed quickly if you decide to rewind a couple of minutes.
If you decide to rewind more frames than are stored in the buffer, it will take more time to reload the frames in which you are searching for.
Basically, if you think you might need quick access to streaming data that has already been observed or used, a circular buffer might be a good solution.
If you enjoyed the article, or want to learn more about computer science and software engineering, follow me here on Medium and checkout my Github and LinkedIn.
Also, here is a link to my implementation of a circular buffer.
SourcesRing buffer basicsThe ring buffer's first-in first-out data structure is useful tool for transmitting data between asynchronous…www.
comCircular Buffer – an overview | ScienceDirect TopicsCircular buffers are useful in DSP programming because most implementations include a loop of some sort.
In the filter…www.
comHow Computer Keyboards WorkA keyboard is a lot like a miniature computer.
It has its own processor and circuitry that carries information to and…computer.
comCircular buffer – Wikipedianeeds additional citations for verification .
improve this article by adding citations to reliable sources.