C - 队列的分段错误在使用dequeue环绕时

时间:2015-02-19 23:06:17

标签: c segmentation-fault queue

好吧,所以我想用Queue创建3个函数。它们是create_queue(),enqueue()和dequeue()。我创建了所有这三个并且正在主要测试它们,如果它已经完全它应该抛出错误以及头部或尾部是否到达它应该环绕的末端。在我的情况下,我得到了enqueue()来环绕但不是dequeue()。我被困在这里试图找出我犯错误的地方。如果有人能发现它并让我知道如何解决它,我将非常感激! ^。^

queuetest.c

#include <stdlib.h>
#include <stdio.h>
#include "queue.h"
#include <errno.h>

typedef struct {
  int x;
  double y;
} Foo; // Just some arbitrary struct


int main() {

  const int max_entries = 4; // size of stack
  Foo* new_foo1;
  Foo* new_foo2;
  Foo* new_foo3;
  Foo* new_foo4;
  Foo* new_foo5;
  Foo* returned_foo;

  // First, create a stack
  Queue *new_queue = create_queue(max_entries);

  // Allocate a Foo and push it onto the queue.
  new_foo1 = (Foo *) malloc(sizeof(Foo));
  new_foo1->x = 100;
  new_foo1->y = 1.11;
  printf("Pushing: x = %5d, y = %10.3f\n", new_foo1->x, new_foo1->y);
  enqueue(new_queue, (void *) new_foo1);

  // Allocate another Foo and push it onto the queue.
  new_foo2 = (Foo *) malloc(sizeof(Foo));
  new_foo2->x = 200;
  new_foo2->y = 2.22;
  printf("Pushing: x = %5d, y = %10.3f\n", new_foo2->x, new_foo2->y);
  enqueue(new_queue, (void *) new_foo2);

  // Allocate another Foo and push it onto the queue.
  new_foo3 = (Foo *) malloc(sizeof(Foo));
  new_foo3->x = 300;
  new_foo3->y = 3.33;
  printf("Pushing: x = %5d, y = %10.3f\n", new_foo3->x, new_foo3->y);
  enqueue(new_queue, (void *) new_foo3);

  // Allocate another Foo and push it onto the queue.
  new_foo4 = (Foo *) malloc(sizeof(Foo));
  new_foo4->x = 400;
  new_foo4->y = 4.44;
  printf("Pushing: x = %5d, y = %10.3f\n", new_foo4->x, new_foo4->y);
  enqueue(new_queue, (void *) new_foo4);

 // Allocate another Foo and push it onto the queue.
  new_foo5 = (Foo *) malloc(sizeof(Foo));
  new_foo5->x = 500;
  new_foo5->y = 5.55;
  printf("Pushing: x = %5d, y = %10.3f\n", new_foo5->x, new_foo5->y);
  enqueue(new_queue, (void *) new_foo5);

  /////////////////////////////////////////////////////////////////

  // Dequeue two  Foos and print them.
  returned_foo = (Foo *) dequeue(new_queue);
  printf("Removed:  x = %5d, y = %10.3f\n", returned_foo->x, returned_foo->y);
  returned_foo = (Foo *) dequeue(new_queue);
  printf("Removed:  x = %5d, y = %10.3f\n", returned_foo->x, returned_foo->y);

  /////////////////////////////////////////////////////////////////

  // Add 3 Foos
  printf("Pushing: x = %5d, y = %10.3f\n", new_foo5->x, new_foo5->y);
  enqueue(new_queue, (void *) new_foo5);
  printf("Pushing: x = %5d, y = %10.3f\n", new_foo1->x, new_foo1->y);
  enqueue(new_queue, (void *) new_foo1);
  printf("Pushing: x = %5d, y = %10.3f\n", new_foo2->x, new_foo2->y);
  enqueue(new_queue, (void *) new_foo2);

  /////////////////////////////////////////////////////////////////

  // Dequeue 5 Foos and print them.
  returned_foo = (Foo *) dequeue(new_queue);
  printf("Removed:  x = %5d, y = %10.3f\n", returned_foo->x, returned_foo->y);
  returned_foo = (Foo *) dequeue(new_queue);
  printf("Removed:  x = %5d, y = %10.3f\n", returned_foo->x, returned_foo->y);
  returned_foo = (Foo *) dequeue(new_queue);
  printf("Removed:  x = %5d, y = %10.3f\n", returned_foo->x, returned_foo->y);
  returned_foo = (Foo *) dequeue(new_queue);
  printf("Removed:  x = %5d, y = %10.3f\n", returned_foo->x, returned_foo->y);
  returned_foo = (Foo *) dequeue(new_queue);
  printf("Removed:  x = %5d, y = %10.3f\n", returned_foo->x, returned_foo->y);

}

queue.c

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "queue.h"

/** Create a queue by allocating a Queue structure, initializing it,
 *  and allocating memory to hold the queue entries.
 * @param max_cells Maximum entries in the queue
 * @return Pointer to newly-allocated Queue structure, NULL if error.
 */
Queue *create_queue(int max_cells) {
  Queue *new_queue; // Holds pointer to the newly-allocated queue structure.
  new_queue = (Queue *) malloc(sizeof(Queue));

  if (new_queue == NULL) return NULL; // Error--unable to allocate.

  // Fill in the struct
  new_queue->max_cells = max_cells;
  new_queue->cells_used = 0; // Empty to start
  new_queue->ePos = 1; //Starts at first position
  new_queue->dPos = 1; //Starts at first position

  // Now allocate space for the queue entries.
  new_queue->head = (void **) calloc(sizeof(void *), max_cells);
  if (new_queue->head == NULL) {
    free(new_queue); // Unable to allocate queue entries, so free struct.
    return NULL;
  }
  new_queue->tail = new_queue->head; // Start at head

  return new_queue;
}

/** Deletes a queue, including the structure and the memory
 * for holding the queue entries, but not the entries themselves.
 * @param which_queue Pointer to Queue structure.
 */
void delete_queue(Queue *which_queue) {
  free(which_queue->head); // Free memory block with queue entries.
  free(which_queue); // Then free the struct.
}

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

 // Check if queue is already full
  if ((which_queue->cells_used) >= (which_queue->max_cells)) {
    which_queue->cells_used = which_queue->max_cells;
    printf("Error: Queue overflow\n");
    return -1;  // Queue overflow.
  }

  // Check if tail is at the end of the Queue
  if ((which_queue->ePos) == (which_queue->max_cells)) {

    //Sets tail to the beginning of queue
    (which_queue->tail) = (which_queue->tail) - (which_queue->max_cells);
    //Sets position back to beginning
    (which_queue->ePos) = 1;
  }

  // Push onto queue.
  *(which_queue->tail) = ptr;  // Store the pointer on the stack
  (which_queue->tail)++;       // Point to next free cell
  (which_queue->cells_used)++;
  (which_queue->ePos)++;
  return 0;  // Success
}

/** Removes head of queue, and returns it.
 * @param which_queue Pointer to Queue you want to dequeue from.
 * @return Head entry of the queue, NULL if queue is empty.
 */
void* dequeue(Queue *which_queue) {

  // Check if queue is empty
  if ((which_queue->cells_used) <= 0) {
    which_queue->cells_used = 0;
    printf("Error: Queue underflow\n");
    return NULL;  // Queue empty
  }

  // Check if head is at the end of the Queue
  if ((which_queue->dPos) == (which_queue->max_cells)) {

    //Sets head to the beginning of queue
    (which_queue->head) = (which_queue->head) - 3;
    //Sets position back to beginning
    (which_queue->dPos) = 1;
    (which_queue->cells_used)--;
    return (*((which_queue->head) + 3));
  }

  // Remove head from queue.
  (which_queue->cells_used)--;
  (which_queue->head)++;
  (which_queue->dPos)++;
  return (*((which_queue->head) - 1));
}

注意:我知道我使用了 - 3和+ 3而不是max_cells - 1等。我只是想看看这是否是一个问题,但它不是。另一个注意事项是,如果我用1 2 3替换-3或+3,它仍然会给我一个seg错误。

queue.h

/** Struct to define a queue; each entry can hold a pointer to anything.
 */
struct queue {
  void **head; // Pointer to head of queue
  void **tail;  // Pointer to next free cell (tail);
  int max_cells; // Maximum number of entries in the queue
  int cells_used; // Currently used number of cells
  int ePos; //Position of the enqueue
  int dPos; //Position of the dequeue
};

typedef struct queue Queue;

// Function prototypes

Queue *create_queue(int max_cells);

void delete_queue(Queue *which_queue);

int enqueue(Queue *which_queue, void *ptr);

void* dequeue(Queue *which_queue);

因此,在调用第二个dequeue()之后,在打印行上发生seg错误。这意味着returned_foo有一个错误的值,这是由于在if语句部分没有正确运行环绕的dequeue函数引起的(至少我认为错误&gt;。&gt;)。任何帮助都会有所帮助!提前谢谢。

1 个答案:

答案 0 :(得分:1)

enqueue()的实现永远不会填充队列中的最后一个元素。

create_queue();  // ePos = 1, queue: x x x x
enqueue(A);      // ePos = 2, queue: A x x x
enqueue(B);      // ePos = 3, queue: A B x x
enqueue(C);      // ePos = 4, queue: A B C x
enqueue(D);      // This already wraps because ePos == max_cells!
                 // ePos = 2, queue: D B C x

dequeue()到达队列的最后一个元素时,它会读取NULL(这是最后一个元素包含的内容,因为calloc用零填充内存块),并且程序崩溃。