首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C程序环缓冲区中的错误

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

Stack Overflow用户
提问于 2017-05-24 03:16:56
回答 2查看 355关注 0票数 0

下面的代码在运行时崩溃。我使用了几个在线引用来编写代码,因为它编译后认为问题在其他地方。但我把剩下的都看了一遍,那似乎很好。

ring_buffer.h

代码语言:javascript
复制
#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

代码语言:javascript
复制
    #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

代码语言:javascript
复制
#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相关的东西。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-05-24 03:32:28

我发现问题是:

代码语言:javascript
复制
memset(&buff_ptr, 0x00, sizeof(buff_ptr));

必须:

代码语言:javascript
复制
memset(buff_ptr, 0x00, sizeof(ring_buffer));
票数 1
EN

Stack Overflow用户

发布于 2017-05-24 03:31:40

代码语言:javascript
复制
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已经是将buff_ptr引用传递给memset的指针,对删除&

代码语言:javascript
复制
memset(&buff_ptr, 0x00, sizeof(buff_ptr));                              // 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44148347

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档