ANSWERS: 1
-
Circular buffers are typically used for producer/consumer relationships, where a small amount of memory is available, and you don't want to deal with dynamic memory allocation for each record, which is the case for real time systems. Conceptually, you can picture a circular buffer as a ring of records, with a head pointer and a tail pointer. When a new record is added, the head pointer is push forward one record, and data is written into it. When a record is read, it is read from the tail pointer, and afterward, the tail pointer is advanced by one record. If the head pointer wraps all the way around to the tail, you've got a buffer overflow condition. If the tail wraps to the head, you've emptied the buffer. The way these are typically implemented is using a fixed-length buffer, whose length is a multiple of the record size (you can deal with variable-length records, but it's more complicated). The code that advances the pointers "knows" when it has hit the end of the buffer and wraps the pointer around to the beginning again. A simple way to do this is just to use the modulo operator on an array index: int idx; idx = (idx + 1) % NUM_ELEMENTS; But there are faster ways (just use an if-statement, for example).
Copyright 2023, Wired Ivy, LLC

by 