c - C程序环形缓冲区中的错误

标签 c

下面的代码在运行时崩溃。我使用了几个在线引用来编写代码,并且由于它编译认为问题出在其他地方。但我检查了其余部分,这似乎工作正常。

环形缓冲区.h

#ifndef RING_BUFFER_H    /* Guard against multiple inclusion */
#define RING_BUFFER_H

#include <stddef.h>                     // Defines NULL
#include <stdbool.h>                    // Defines true
#include <stdlib.h>                     // Defines EXIT_FAILURE
#include <stdint.h>                     // Defines uint32_t
#include <string.h>


typedef struct{
    uint8_t * buffer;
    uint8_t head;
    uint8_t tail;
    uint8_t max_length;
}ring_buffer;

void buffer_init(ring_buffer *buff_ptr);

bool buffer_full(ring_buffer *buff_ptr);

bool buffer_empty(ring_buffer *buff_ptr);

bool buffer_write(ring_buffer *buffer, uint8_t data);

bool buffer_read(ring_buffer *buffer, uint8_t * data);

#endif /* _EXAMPLE_FILE_NAME_H */

环缓冲区.c

    #include "ring.h"

//ring_buffer UART_buffer; this goes in UART.h

#define UART_RING_BUFFER_SIZE 16

void buffer_init(ring_buffer *buff_ptr)
{                                                                           // type casting cause malloc returns void pointer
    buff_ptr = (ring_buffer*)malloc(sizeof(ring_buffer));                   // assign address to the uart_ring_buffer of size ring_buffer
    memset(&buff_ptr, 0x00, sizeof(buff_ptr));                              // set all locations to NULL so that if read before write conditions occur, garbage data is not read

    buff_ptr->buffer = (uint8_t*)malloc(UART_RING_BUFFER_SIZE * sizeof(uint8_t));   // data buffer assigned of size max_length
    memset(&buff_ptr->buffer, 0x00, sizeof(uint8_t));

    buff_ptr-> head = 0;
    buff_ptr-> tail = 0;
    buff_ptr-> max_length = UART_RING_BUFFER_SIZE;
}


bool buffer_write(ring_buffer *buff_ptr, uint8_t data)
{
    int next = buff_ptr->head + 1; // increment head to point to location in which data will be written to
    if(next >= buff_ptr->max_length)
        next = 0;
    if(next == buff_ptr->tail) //check for buffer full condition
        return -1;              // indicate write failed, buffer full

    buff_ptr->buffer[buff_ptr->head] = data;
    buff_ptr->head = next; // update head to point to current location

    return 0;                   // indicates buffer write success
}

bool buffer_read(ring_buffer *buff_ptr, uint8_t *data)
{
    int next = buff_ptr->tail+1;

    if(next >= buff_ptr->max_length)
        next = 0;

    if(buff_ptr->head == buff_ptr->tail) // check for buffer empty
        return -1; // indicates read failed, buffer empty

    *data = buff_ptr->buffer[buff_ptr->tail];
    buff_ptr->tail = next;
    return 0;
}

bool buffer_full(ring_buffer *buff_ptr) //NOT PROPER LOGIC
{
    int next = buff_ptr->head + 1; // increment head to point to location in which data will be written to
    if(next >= buff_ptr->max_length)
        next = 0;
    if(next == buff_ptr->tail) //check for buffer full condition
        return 1;
    else
        return 0;
}

bool buffer_empty(ring_buffer *buff_ptr)
{
    if(buff_ptr->head == buff_ptr->tail) // check for buffer empty
        return 1; // indicates read failed, buffer empty
    return 0;
}

主.c

#include <stdio.h>
#include <stdlib.h>
#include "ring.h"


ring_buffer UART_FIFO;
int main()
{

    char x;
    buffer_init(&UART_FIFO);



    printf("data in goes here: ");
    while (1)
        {
            scanf("%c",&x);
            buffer_write(&UART_FIFO, x);
        }
}

让我知道是否有任何明显的错误,对使用指针有点陌生,以前做过 verilog 和 FPGA 相关的东西。

最佳答案

我发现了问题:

memset(&buff_ptr, 0x00, sizeof(buff_ptr));

必须是:

memset(buff_ptr, 0x00, sizeof(ring_buffer));

关于c - C程序环形缓冲区中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44148347/

相关文章:

c - 如何从 bash 终端获取输入到我的程序中

c - NASM 数组指针操作

c - 为什么 glibc 的 fork 实现没有使用 sys_fork?

c# - 将 C# 集成到 C 到 Java 时出现 OutOfMemoryError/程序崩溃

c - 如何为多线程生产者-消费者 C 程序编写测试?

c++ - 'inet_ntoa' 未声明错误

c - 将C代码转换为 Octave

c - C 中的 union 赋值

c - C 可变参数函数与 Fortran 的互操作性

c - 更新文件中的记录文件