Circular buffer is a queueing mechanism with the help of a fixed size array often used in low memory footprint systems and device drivers.
Circular buffer can be implemented with static array or fixed size dynamic array.
It never calls any dynamic memory allocation routines like alloc/free/resize during runtime. It manages addition and deletion of queue elements in a circular rotational fashion.
Mechanism and Implementation
It has a fixed size array to hold elements. It has two memebrs called start and end to pint the index in the array to determine front and back of the queue.
It also has the size memeber to save element count. At the beggining we set start and end both to zero index.
End will increment as new elements gets added and start will be incremented when a oldet elemnt gets removed. Thse increments are circular and never crosses beyond max array index.
They rounded off to the starting as we do a modulo. Now end may get back to start when buffer is entirely full. So start and end will point to same index when buffer is either full or empty.
We often use a count to distinguish empty and full situation.
Simple logic using array and no complex data structures and logic used
No dynamic allocation or deallocation and no chance of memory leak and other issues
Best suit for low memory footprint systems or for device drivers
Very fast operations and donot include delay of malloc and free
These can be used in applications as well as low level codes in interrupt handlers and realtime systems
Queue is fixed fixed size and this cannot be changed
addition of new element will overwrite old queue elements or has to wait till some elements are free