以下代码在运行时崩溃。我使用了几个在线参考来提出代码,因为它编译思想问题是在其他地方。但我完成了其余部分,这似乎工作正常。
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
#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
#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相关的事情。
答案 0 :(得分:1)
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的引用并没有做任何好事删除&amp;
memset(&buff_ptr, 0x00, sizeof(buff_ptr)); //
答案 1 :(得分:1)
我发现了这个问题:
memset(&buff_ptr, 0x00, sizeof(buff_ptr));
必须:
memset(buff_ptr, 0x00, sizeof(ring_buffer));