我試圖通過C語言中的數組和指針實現隊列。隊列 被C結構C指針隊列中的指針
// Circular Buffer
typedef struct{
main_controller_req_t buffer[BUFFER_SIZE]; // buffer
uint16_t size; // length of the queue
uint16_t count; // number of elements present in the queue
main_controller_req_t *p_head; // pointer to head of the queue (read end)
main_controller_req_t *p_tail; // pointer to tail of the queue (write end)
}circular_buffer_t;
我已經實現功能,用於隊列初始化
void init_cb(circular_buffer_t *p_cb){
p_cb->p_head = p_cb->buffer;
p_cb->p_tail = p_cb->buffer;
p_cb->count = 0;
p_cb->size = BUFFER_SIZE;
}
和功能,用於插入到所述隊列建模
BOOL enqueue_cb(circular_buffer_t *p_cb, main_controller_req_t *p_enq_elem){
if(p_cb->count < p_cb->size){
// queue contains at least one free element
taskENTER_CRITICAL();
// insert the element at the tail of queue
*(p_cb->p_tail) = *p_enq_elem;
// incrementing modulo size
p_cb->p_tail = (((p_cb->p_tail++) == (p_cb->buffer + p_cb->size)) ? (p_cb->buffer) : (p_cb->p_tail));
// one element added
p_cb->count++;
taskEXIT_CRITICAL();
return TRUE;
}else{
// queue is full
return FALSE;
}
}
的enqueue_cb功能運作良好,直到尾部達到BUFFER_SIZE。然後 程序崩潰,uC重置。問題是在p_tail指針更新,但我不明白爲什麼 。請任何人都可以幫助我?提前致謝。
我建議你在你的電腦(Visual Studio或類似的)上執行所有不依賴於CPU的庫。它將減輕您的調試併產生更少的問題。 – tilz0R
請提供您的使用導致崩潰的主要功能的一小部分。 –
@Steve>你不能在同一語句中分配p_cb-> p_tail **和**使用'++'。 (很公平,你可以,你只是讓你的代碼難以理解,即使是你自己也是如此) – spectras