循环缓冲区在第 6 个元素之后没有给出正确的缓冲区大小

Circular buffer does not give the correct size of the buffer after 6-th element

我已经用C 编写了循环缓冲区的代码,并且在某种程度上它运行良好。我将缓冲区的大小设为 10。当我填充缓冲区直到元素 6 - 它工作正常。但是在我填充第 7 个元素的那一刻 - 我得到结果“缓冲区的大小等于 767”。对于元素 8 - 它不起作用。我用“头”来写,用“尾”来提取值。你能帮我解决这个问题吗?

#include<stdio.h>
#include<stdint.h>
#include <stdbool.h>


typedef struct RingBuffer {
   uint16_t* buffer;
   size_t head;
   size_t tail;
   size_t max;
   bool full;

}*cbuf_handle_t;

cbuf_handle_t init_RingBuffer (uint8_t* buffer, size_t size){

   cbuf_handle_t cbuf = malloc (sizeof(cbuf_handle_t));
   cbuf->buffer = buffer;
   cbuf->max = size;
   return cbuf;

}


void RingBuffer_free(cbuf_handle_t cbuf){

   free(cbuf);
}

void RingBuffer_reset(cbuf_handle_t cbuf){
   cbuf->head = 0;
   cbuf->tail = 0;
   cbuf->full = false;
   }

bool RingBuffer_full (cbuf_handle_t cbuf){

   return cbuf->full;
}

bool RingBuffer_empty(cbuf_handle_t cbuf){

       return (!cbuf->full && (cbuf->tail == cbuf->head));
}

size_t RingBuffer_Capacity(cbuf_handle_t cbuf){
   return cbuf->max;
}

size_t RingBuffer_size(cbuf_handle_t cbuf){
   size_t size = cbuf->max;

   if (!cbuf->full){
   if (cbuf->head >= cbuf->tail)
       {
       size = (cbuf->head - cbuf->tail);}
       else
       {
       size = (cbuf->head - cbuf->tail + cbuf->max);
       }
       }
        return size;
   }



void RingBuffer_AdvancePointer(cbuf_handle_t cbuf){
   if (cbuf->full){
       cbuf->tail = (cbuf->tail+1)%cbuf->max;
   }

   cbuf->head = (cbuf->head + 1)%cbuf->max;
   cbuf->full = (cbuf->head == cbuf->tail);
}

void RingBuffer_retreatPointer (cbuf_handle_t cbuf){
   cbuf->full = false;
   cbuf->tail = (cbuf->tail + 1)%cbuf->max;
}

void RingBuffer_addValue (cbuf_handle_t cbuf, uint8_t data){
   cbuf->buffer[cbuf->head] = data;
   RingBuffer_AdvancePointer(cbuf);


   }

int RingBuffer_Remove (cbuf_handle_t cbuf, uint8_t *data){
   int r = -1;
   if (!RingBuffer_empty(cbuf)){
       *data = cbuf->buffer[cbuf->tail];
       RingBuffer_retreatPointer(cbuf);
       r = 0;
   }
   return r;
}



int main (){



uint8_t arr[10];
cbuf_handle_t cpt = init_RingBuffer(arr, 10);

//initialzie the buffer, tail and head and max

int i = 0;
RingBuffer_reset(cpt);


for ( i = 0 ; i< 6; i++){
   RingBuffer_addValue(cpt, i);
}


size_t size = RingBuffer_size(cpt);
printf("The size of the buffer %d", size);
}


提前致谢!

此致

罗斯季斯拉夫

如评论中所述,通常不建议将结构声明为指针。但是,您可以通过更改使用 malloc 分配它的方式来修复该错误:

cbuf_handle_t cbuf = malloc (sizeof(*cbuf));

这是因为,cbuf 是指向结构的指针,如果您取消引用它,您将获得结构,因此在将它传递给 sizeof 时会得到它的实际大小。