Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C程序环形缓冲区出错_C - Fatal编程技术网

C程序环形缓冲区出错

C程序环形缓冲区出错,c,C,下面的代码在运行时崩溃。我使用了一些在线参考资料来编写代码,由于它是编译的,所以我认为问题出在其他地方。但我把剩下的都看了一遍,似乎效果不错 环形缓冲区 #ifndef RING_BUFFER_H /* Guard against multiple inclusion */ #define RING_BUFFER_H #include <stddef.h> // Defines NULL #include <stdbool.h&g

下面的代码在运行时崩溃。我使用了一些在线参考资料来编写代码,由于它是编译的,所以我认为问题出在其他地方。但我把剩下的都看了一遍,似乎效果不错

环形缓冲区

#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 */
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);
        }
}
#包括
#包括
#包括“ring.h”
环形缓冲UART FIFO;
int main()
{
字符x;
缓冲区初始化(UART\U FIFO);
printf(“数据在此显示:”);
而(1)
{
scanf(“%c”、&x);
缓冲区写入(&UART\U FIFO,x);
}
}
让我知道,如果有任何明显的错误,有点新的使用指针,以前做过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
该buff_ptr已经是一个指针,它将buff_ptr的引用传递给memset,这对移除&

memset(&buff_ptr, 0x00, sizeof(buff_ptr));                              // 
该buff_ptr已经是一个指针,它将buff_ptr的引用传递给memset,这对移除&

memset(&buff_ptr, 0x00, sizeof(buff_ptr));                              // 
我发现了这个问题:

memset(&buff_ptr, 0x00, sizeof(buff_ptr));
必须是:

memset(buff_ptr, 0x00, sizeof(ring_buffer));
我发现了这个问题:

memset(&buff_ptr, 0x00, sizeof(buff_ptr));
必须是:

memset(buff_ptr, 0x00, sizeof(ring_buffer));
#包括“环h”??当您的头文件显示ring_buffer.h时,是否包含“ring.h”??而您的头文件显示ring_buffer.h