环形缓冲区

背景:

循环存储大量数据,实现新的数据自动覆盖老的数据,最简单方法。


实现代码:

定义类型:

包括一个用来指向存储数据的数组指针buffer,记录头部的位置head,记录尾部的位置tail,开辟缓存的容量capacity,以及数据满的标志位full。

复制代码
typedef struct{
    int *buffer;
    size_t head;
    size_t tail;
    size_t capacity;
    bool full;
}CircularBuffer;

创建:

一个环形缓存区,初始化结构体的相关参数,开辟相应capacity的buffer。

复制代码
CircularBuffer * create(size_t capacity)
{
    CircularBuffer *cb = (CircularBuffer *)malloc(sizeof(CircularBuffer));
    if(!cb)
        return NULL;
    cb->buffer = (int *)malloc(capacity *sizeof(int));
    if(!cb->buffer){
        free(cb);
        return NULL;
    }
    cb->head = 0;
    cb->tail = 0;
    cb->capacity = capacity;
    cb->full = false;
    return cb;
}

释放:

释放环形缓冲区的内存。

复制代码
void destroy(CircularBuffer  *cb)
{
    if(cb){
        free(cb->buffer);
        free(cb);
    }
}

读取:

从缓冲区读取元素,若为空则返回错误值。

复制代码
int get(CircularBuffer  *cb)
{
    if(isEmpty(cb))
        return -1;
    int item = cb->buffer[cb->head];
    cb->full = false;
    cb->head = (cb->head + 1) % cb->capacity;
    return item;
}

判空满:

检测缓冲区是否为空或已满。

复制代码
bool isEmpty(CircularBuffer  *cb)
{
    return (!cb->full && cb->tail == cb->head);
}

bool isFull(CircularBuffer  *cb)
{
    return cb->full;
}

获取容量和大小:

获取缓冲区的容量和当前大小。

复制代码
size_t capacity(CircularBuffer  *cb)
{
    return cb->capacity;
}

size_t size(CircularBuffer  *cb)
{
    if(cb->full)
        return cb->capacity;
    if(cb->tail >= cb->head)
        return cb->tail - cb->head;
    return cb->capacity + cb->tail - cb->head;
}

写入:

向缓冲区添加新元素。如果缓冲区已满,覆盖最早的数据。

复制代码
void put(CircularBuffer *cb, int item)
{
    cb->buffer[cb->tail] = item;
    if(cb->full){
        cb->head = (cb->head + 1) % cb->capacity;
    }
    cb->tail = (cb->tail + 1) % cb->capcity;
    cb->full = cb->tail == cb->head;
}
复制代码

使用解释:

复制代码
int main()
{
    CircularBuffer  *buf = create(5);
    
    put(buf,1);
    put(buf,2);
    put(buf,3);

    while (!sEmpty(buf)) {
        int item = get(buf);
        printf("%d\n", item);
    }

    destroy(buf);
    return 0;
}

理解环形缓冲区的关键在于理解 tail 指针的更新方式。cb->tail = (cb->tail + 1) % cb->capacity; 这一行代码的作用是将 tail 指针移动到下一个写入位置,并确保 tail 指针在缓冲区范围内循环。

tail 指针的作用
  • tail 指针:指向缓冲区中下一个要写入的位置。
  • head 指针:指向缓冲区中下一个要读取的位置。
为什么需要 (cb->tail + 1) % cb->capacity
  • cb->tail + 1 :将 tail 指针向前移动一位。
  • % cb->capacity :使用模运算确保 tail 指针在缓冲区范围内循环。
为什么需要模运算
  • 模运算 :确保 tail 指针在缓冲区范围内循环。
  • tail 指针达到缓冲区的末尾时,tail = (tail + 1) % capacity 会将 tail 指针重置为 0,从而实现循环写入。

示例:

假设缓冲区的容量为 capacity = 5,初始状态为空(head = 0, tail = 0, full = false),依次插入元素 1, 2, 3, 4, 5

  1. 插入 1

    • 写入位置:buffer[0] = 1
    • 更新 tailtail = (0 + 1) % 5 = 1
    • 状态:head = 0, tail = 1, full = false
  2. 插入 2

    • 写入位置:buffer[1] = 2
    • 更新 tailtail = (1 + 1) % 5 = 2
    • 状态:head = 0, tail = 2, full = false
  3. 插入 3

    • 写入位置:buffer[2] = 3
    • 更新 tailtail = (2 + 1) % 5 = 3
    • 状态:head = 0, tail = 3, full = false
  4. 插入 4

    • 写入位置:buffer[3] = 4
    • 更新 tailtail = (3 + 1) % 5 = 4
    • 状态:head = 0, tail = 4, full = false
  5. 插入 5

    • 写入位置:buffer[4] = 5
    • 更新 tailtail = (4 + 1) % 5 = 0
    • 状态:head = 0, tail = 0, full = true(缓冲区已满)

这时缓冲区已满(head = 0, tail = 0, full = true),再插入一个元素 6

  1. 写入位置:buffer[0] = 6(覆盖最早的元素 1
  2. 更新 tailtail = (0 + 1) % 5 = 1
  3. 更新 headhead = (0 + 1) % 5 = 1(因为缓冲区已满,需要移动 head 指针)
  4. 状态:head = 1, tail = 1, full = true

最后:

通过这种方式,环形缓冲区可以有效地循环利用固定大小的数组空间,避免数据溢出。

相关推荐
421!20 小时前
C语言学习笔记——10(结构体)
c语言·开发语言·笔记·stm32·学习·算法
不只会拍照的程序猿21 小时前
《嵌入式AI筑基笔记04:python函数与模块01—从C的刻板到Python的灵动》
c语言·开发语言·笔记·python
我叫洋洋21 小时前
[STM32 和 PWM 输出 结合 proteus 仿真]
stm32·嵌入式硬件·proteus
凌盛羽21 小时前
ESP32-S3定时器组Timer Group0/1的使用
stm32·单片机·嵌入式硬件·链表·esp32·定时器
2301_7890156221 小时前
C++11新增特性:列表初始化&左值引用&右值引用&万能引用&移动构造&移动赋值&引用折叠&完美转发
c语言·开发语言·c++·c++11
我不是程序猿儿21 小时前
【嵌入式】第2讲:USB CDC 从“插上电脑”到“出现 COM 口”,枚举过程到底发生了什么
服务器·stm32·单片机·嵌入式硬件·电脑·负载均衡
学嵌入式的小杨同学1 天前
STM32 进阶封神之路(三十三):W25Q64 任意长度写入深度实战 —— 从页限制到工业级通用读写(附完整代码 + 避坑指南)
stm32·单片机·嵌入式硬件·架构·硬件架构·嵌入式·flash
三道渊1 天前
C语言:文件I/O
c语言·开发语言·数据结构·c++
kali-Myon1 天前
CTFshow-Pwn142-Off-by-One(堆块重叠)
c语言·数据结构·安全·gdb·pwn·ctf·