【发布时间】:2020-11-10 11:40:41
【问题描述】:
我正在使用 72kB 闪存和 16kB RAM 控制器。 IDE:Keil uVision v5.29 IDE。
我正在创建一个循环缓冲区,它是一个指向结构的指针。
malloc 正在创建一个硬故障。代码:
/*** Header File *****/
typedef struct circular_structbuf_t circular_structbuf_t;
typedef circular_structbuf_t* scbuf_handle_t;
/****** C file *****/
struct main_struct
{
int data;
char char_data;
}
struct circular_structbuf_t
{
struct main_struct st_array[128];
uint8_t st_head;
uint8_t st_tail;
uint8_t st_max; //of the buffer
bool st_full;
};
scbuf_handle_t circular_structbuf_init(scbuf_handle_t scbuf, size_t size)
{
scbuf = malloc(sizeof(circular_structbuf_t)); // Causes Hardfault
scbuf->st_max = size;
circular_structbuf_reset(scbuf);
return scbuf;
}
/** Main File ***/
scbuf_handle_t p_cbuf;
int main(void)
{
p_cbuf=circular_structbuf_init(p_cbuf,50);
}
调试时,p_cbuf的地址被分配为0x0。
【问题讨论】:
标签: struct malloc keil circular-buffer