Sign in to follow this  
pressgreen

Circular buffer usage

Recommended Posts

pressgreen    678

So i am reading about the Circular Buffer. I personally have never used one but it seemed intriguing. Could some one share some scenarios for its optimal usage vs regular delineated buffer, for those of you that are familiar with its usefulness. Like when is it appropriate vs not appropriate?

Share this post


Link to post
Share on other sites
Mussi    4407

Lets say you have a game with a rewind functionality, where your character can travel back in time. You'll most likely have a cap on how far one can rewind. At any point in time, you'll have to know the x steps taken before the current step. Now imagine how easy this becomes with a circular buffer smile.png.

Share this post


Link to post
Share on other sites
SillyCow    1461

Having a circular buffer prevents you from allocating new memory.

It also prevents memory  fragmentation.

Both of these problems will cause performance drops.

 

Any task that involving queues where performance is an issue will benefit from a circular buffer.

 

They are also in use in hardware and embedded programming.

Here, memory allocation (new / malloc ) is sometimes not available.

Share this post


Link to post
Share on other sites

Yeah, as I said anything a double ended queue is good for is ideal (as long as the queue has a fixed maximum size - although it can be resized of course. For efficiency reasons you will probably prefer a buffer size which is a power of 2 so you can use logical AND rather than modulo arithmetic to wrap the buffers).

 

More use-cases:

 

Message system (read messages from front of queue add new messages to the back of the queue).

Bubble Bobble (Puzzle Bobble? Maybe?) scrolling-type games where new tiles are added to the top of the screen and tiles are removed from the bottom of the screen.

Rewind buffer (already mentioned)

Text window with limited history (already mentioned)

Undo buffer with limited undo

 

etc.

Edited by Paradigm Shifter

Share this post


Link to post
Share on other sites
prh99    520

I know they are used in LZMA and LZSS compression as way to create a sliding window for the algorithm and in various aspects of digital signal processing.

Edited by prh99

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this