int queue_is_full(Queue *queue) { return ((queue->tail + 1) % queue->size) == (queue->head % queue->size); } 如何将头指针和尾指针对队列长度取模,以防止指针越界。
时间: 2024-03-10 14:45:16 浏览: 17
在进行指针操作时,需要对队列长度取模,以确保指针不会越界。具体来说,假设队列长度为N,当队列的头指针或尾指针超过N-1时,需要对其进行取模操作,将其限制在0至N-1之间。这可以通过使用取模运算符%来实现。例如,如果要获取头指针对长度N取模后的结果,可以使用以下代码:
```
queue->head % queue->size
```
这样可以保证头指针始终在0至N-1之间,不会越界。同样的,对于尾指针,也可以使用相同的方法进行取模操作。
相关问题
把这个结构体更改命名为charbuff,并修改下列函数变量typedef struct { uint8_t *data; //!< Array of data, stored on the heap. uint32_t head; //!< Index in the array of the oldest element. uint32_t tail; //!< Index in the array of the youngest element. uint32_t size; //!< Size of the data array. } Queue; Queue charBuf; int queue_init(Queue *queue) { const uint32_t size = 4; queue->data = (uint8_t*)malloc(sizeof(uint8_t) * size); queue->head = 0; queue->tail = 0; queue->size = size; // If malloc returns NULL (0) the allocation has failed. return queue->data != 0; } int queue_is_full(Queue *queue) { return ((queue->tail + 1) == queue->head; } int queue_is_empty(Queue *queue) { return queue->tail == queue->head; } int queue_enqueue(Queue *queue, uint8_t item) { if (!queue_is_full(queue)) { queue->data[queue->tail++] = item; queue->tail %= queue->size; return 1; } else { uart_disable(); return 0; } } int queue_dequeue(Queue *queue, uint8_t *item) { if (!queue_is_empty(queue)) { *item = queue->data[queue->head++]; queue->head %= queue->size; return 1; } else { return 0; } }
如果将结构体重命名为`charbuff`,可以将代码修改为如下形式:
```
typedef struct {
uint8_t *data; //!< Array of data, stored on the heap.
uint32_t head; //!< Index in the array of the oldest element.
uint32_t tail; //!< Index in the array of the youngest element.
uint32_t size; //!< Size of the data array.
} charbuff;
charbuff charBuf;
int queue_init(charbuff *queue) {
const uint32_t size = 4;
queue->data = (uint8_t*)malloc(sizeof(uint8_t) * size);
queue->head = 0;
queue->tail = 0;
queue->size = size;
// If malloc returns NULL (0) the allocation has failed.
return queue->data != 0;
}
int queue_is_full(charbuff *queue) {
return ((queue->tail + 1) == queue->head);
}
int queue_is_empty(charbuff *queue) {
return queue->tail == queue->head;
}
int queue_enqueue(charbuff *queue, uint8_t item) {
if (!queue_is_full(queue)) {
queue->data[queue->tail++] = item;
queue->tail %= queue->size;
return 1;
} else {
uart_disable();
return 0;
}
}
int queue_dequeue(charbuff *queue, uint8_t *item) {
if (!queue_is_empty(queue)) {
*item = queue->data[queue->head++];
queue->head %= queue->size;
return 1;
} else {
return 0;
}
}
```
这样就将结构体重命名为`charbuff`,并将函数参数中的`Queue`类型改为`charbuff`类型。这样做可以使代码更加清晰易懂,减少歧义和误解。同时,也需要将变量名统一修改为`charBuf`。
设置这个队列长度为8的时候判为满int queue_is_full(Queue *queue) { return ((queue->tail + 1) % queue->size) == queue->head; } int queue_is_empty(Queue *queue) { return queue->tail == queue->head; } int queue_enqueue(Queue *queue, uint8_t item) { if (!queue_is_full(queue)) { queue->data[queue->tail++] = item; queue->tail %= queue->size; return 1; } else { uart_disable(); return 0; } } int queue_dequeue(Queue *queue, uint8_t *item) { if (!queue_is_empty(queue)) { *item = queue->data[queue->head++]; queue->head %= queue->size; return 1; } else { return 0; } }
如果设置这个队列长度为8,那么队列满的判断应该是:
```
int queue_is_full(Queue *queue) {
return ((queue->tail + 1) % queue->size) == (queue->head % queue->size);
}
```
这里需要将头指针和尾指针对队列长度取模,以防止指针越界。