C基本环缓冲区问题

时间:2016-03-08 14:05:04

标签: c buffer ring

我正在尝试使用基本的环形缓冲区。 我似乎无法正确地包裹它。 当我达到elements = buffer_length的数量时,行为变得不可预测,并且在调试器中它只显示第一个*元素。

任何人都可以看到我遗漏的明显错误。

提前致谢。

#include "stdafx.h"
#include <stdio.h>
#include <string.h>

#define BUFFER_LENGTH 8

typedef struct circular_buffer
{
    float buffer[BUFFER_LENGTH];
    float *buffer_end;
    float *head;
    float *tail;
    size_t count;
} circular_buffer;

circular_buffer buffer;

void cb_init(circular_buffer *cb)
{
    memset(cb->buffer, 0, BUFFER_LENGTH * sizeof(float));

    cb->buffer_end = (float *)cb->buffer + BUFFER_LENGTH * sizeof(float);
    cb->count = 0;
    cb->head = cb->buffer;
    cb->tail = cb->buffer;
}

void cb_push_back(circular_buffer *cb, float item)
{
    *cb->head = item;
    cb->head = (float *)cb->head++;
    if (cb->head == cb->buffer_end)
        cb->head = &cb->buffer[0];
    cb->count++;
}

float cb_pop_front(circular_buffer *cb)
{
    float item = 0;
    item = *cb->tail;
    cb->tail = (float*)cb->tail++;
    if (cb->tail == cb->buffer_end)
        cb->tail = &cb->buffer[0];
    cb->count--;
    return item;
}

float cb_peek_front(circular_buffer *cb, size_t pos)
{
    float *arrPos;
    size_t i = 0;
    do
    {
        arrPos = (float*)cb->tail + i;
        if (arrPos == cb->buffer_end)
            arrPos = cb->buffer;
        i++;
    } while (i <= pos);

    return *arrPos;

}

int _tmain(int argc, _TCHAR* argv[])
{
    cb_init(&buffer);

    cb_push_back(&buffer, 5);
    printf("%f\n",cb_pop_front(&buffer));
    cb_push_back(&buffer, 6);
    printf("%f\n", cb_pop_front(&buffer));
    cb_push_back(&buffer, 7);
    printf("%f\n", cb_pop_front(&buffer));
    cb_push_back(&buffer, 8);
    printf("%f\n", cb_pop_front(&buffer));
    cb_push_back(&buffer, 9);
    printf("%f\n", cb_pop_front(&buffer));
    cb_push_back(&buffer, 10);
    printf("%f\n", cb_pop_front(&buffer));
    cb_push_back(&buffer, 11);
    printf("%f\n", cb_pop_front(&buffer));
    cb_push_back(&buffer, 12);
    printf("%f\n", cb_pop_front(&buffer));

    // stops working here
    cb_push_back(&buffer, 13);
    printf("%f\n", cb_pop_front(&buffer));

    cb_push_back(&buffer, 14);
    printf("%f\n", cb_pop_front(&buffer));


    return 0;
}

2 个答案:

答案 0 :(得分:1)

这个

cb->buffer_end = (float *)cb->buffer + BUFFER_LENGTH * sizeof(float);

不会做你期望的事。

将其更改为

cb->buffer_end = cb->buffer + BUFFER_LENGTH;

让它指向缓冲区的最后一个元素。

此外,只需将所有无用的演员表移至(float *)

此外,您想要替换

cb->head = (float *)cb->head++;

只是一个简单的

cb->head++;

cb->tail = (float*)cb->tail++;

通过

cb->tail++;

答案 1 :(得分:1)

使用指针运算时,你正在考虑元素的大小。例如

List<T>

前进到下一个元素,就像使用数组索引一样,您也不需要考虑每个元素的大小。所以

bufpointer += 1;

变为

cb->buffer_end = (float *)cb->buffer + BUFFER_LENGTH * sizeof(float);