gpt4 book ai didi

检查相等指针和内存汇总

转载 作者:行者123 更新时间:2023-11-30 17:20:15 24 4
gpt4 key购买 nike

所以,我正在使用 C 中的结构和指针实现队列:

注意:我的程序使用 max_cells = 3。

这是我的队列.h:

struct queue {
int max_cells; // Maximum number of cells in the queue
int cells_used; // Number of cells used
void **head; // Pointer to the head of queue
void **tail; // Pointer to the tail of queue
void **queue_base; // Pointer to the base of the queue
};

typedef struct queue Queue; // For convenience

这是我的排队代码:

/**
* Enqueue a pointer into the queue
* @param which_queue Pointer to Queue you want to push onto
* @param ptr Pointer to be pushed
* @return 1 if successful, 0 if not
*/
int enqueue(Queue *which_queue, void *ptr) {

// If the Queue is already full, print a message and terminate the function
if ((which_queue->cells_used) == (which_queue->max_cells)) {
printf("The queue is full. It can only contains %d cells.\n", which_queue->max_cells);
return 0;
}

// Otherwise, enqueue the cell that head points to
// Checking tail++ to see if it goes off the queue
// If it does, then make tail point to the queue_base
if (((which_queue->tail)++) == ((which_queue->queue_base) + (which_queue->max_cells))) {
which_queue->tail = which_queue->queue_base;
} else {
// Push the pointer to the queue
*(which_queue->tail) = ptr;
// Point to the next free cell
(which_queue->tail)++;
// Then, increase the number of cells used in the Queue
(which_queue->cells_used)++;
}
return 1; // Indicate success
}

我通过打印出每个指向的内存位置来检查我的程序:(Foo 只是随机结构)

Enqueued new_foo1.
Current number of cells in the queue: 1
Head pointer is pointing to: 0x147d040
Tail pointer is pointing to: 0x147d050
Base pointer is pointing to: 0x147d040
Enqueued new_foo2.
Current number of cells in the queue: 2
Head pointer is pointing to: 0x147d040
Tail pointer is pointing to: 0x147d060
Enqueued new_foo3.
Current number of cells in the queue: 3
Head pointer is pointing to: 0x147d040
Tail pointer is pointing to: 0x147d070
The queue is full. It can only contains 3 cells.
-----------------------
Dequeued new_foo1
Current number of cells in the queue: 2
Head pointer is pointing to: 0x147d050
Tail pointer is pointing to: 0x147d070
Enqueued new_foo4
Current number of cells in the queue: 3
Head pointer is pointing to: 0x147d050
Tail pointer is pointing to: 0x147d080
*** (which_queue->queue_base) + (which_queue->max_cells) = 0x147d058
Dequeued new_foo2
Current number of cells in the queue: 2
Head pointer is pointing to: 0x147d060
Tail pointer is pointing to: 0x147d080
Dequeued new_foo3
Current number of cells in the queue: 1
Head pointer is pointing to: 0x147d070
Tail pointer is pointing to: 0x147d080

问题出在标有***的行上。为什么会出现这种情况?我认为内存位置应该是0x147d070。

请帮助我。谢谢。

最佳答案

您还需要更明确地了解尾指针的具体用途——它是指向添加到队列中的最后一个条目,还是指向添加到队列中的最后一个条目之后的可用空间?有些人喜欢用一种方式做,有些人喜欢用另一种方式做。

由于您首先递增尾指针,这意味着您希望尾指针指向最后添加的条目。但正如 rpattiso 已经指出的那样,在使用指针将条目存储到队列中之后,您不应该再增加指针。

另一件事 - 如果您以这种方式使用尾指针,请确保在向空队列添加内容时正确设置头指针。

关于检查相等指针和内存汇总,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28647879/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com