簡體   English   中英

檢查相等指針和內存綜述

[英]Checking Equal Pointers & Memory Roundup

所以,我在C中使用struct和pointer實現隊列:

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

這是我的queue.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已經指出的那樣,在使用它來將條目存儲到隊列中之后,你也不應該增加指針。

另一件事 - 如果你以這種方式使用尾指針,請確保在向空隊列添加某些內容時正確設置了頭指針。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM