简体   繁体   English

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

[英]error in C program ring buffer

The code below crashes during runtime. 下面的代码在运行时崩溃。 I used a couple of online references to come up with the code, and since it compiles thought the issue was somewhere else. 我使用了两个在线参考来提出代码,并且由于编译后认为问题出在其他地方。 But I went over the rest of it and that seems to work fine. 但我仔细阅读了其余内容,看来效果很好。

ring_buffer.h ring_buffer.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 */

ring_buffer.c ring_buffer.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;
}

main.c main.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);
        }
}

let me know if there are any obvious mistakes, kinda new to using pointers, have done verilog and FPGA related stuff previously. 让我知道是否存在任何明显的错误,这是使用指针的一种新东西,之前已经完成了Verilog和FPGA相关的工作。

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

that buff_ptr is already a pointer passing reference of buff_ptr to memset doesn't do any good remove the & buff_ptr已经是一个将buff_ptr的引用传递给memset的指针,对删除&并没有任何帮助。

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

I found the issue: 我发现了问题:

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

Has to be: 必须:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM