C 队列研磨错误

C 队列研磨错误,c,queue,valgrind,C,Queue,Valgrind,这个代码似乎有效。但是,以下代码会导致Valgrind错误: #include <stdlib.h> #include "queue.h" #include "queuepriv.h" #include <string.h> Queue *Queue_init(void) { Queue *q = calloc(1, sizeof(Queue)); return q; } int Queue_enqueue(Queue *q, const char *

这个代码似乎有效。但是,以下代码会导致Valgrind错误:

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

Queue *Queue_init(void)
{
    Queue *q = calloc(1, sizeof(Queue));
    return q;
}

int Queue_enqueue(Queue *q, const char *id, const char *name)
{
    // implement this function
    struct student *new = calloc(1, sizeof(struct student));
    if (strlen(id) <= 6) {
      strcpy(new->id, id);
      new->name = malloc(strlen(name) + 1);
      strcpy(new->name, name);
      new->name[strlen(name)] = '\0';
      if (q->last)
        q->last->next = new;
      q->last = new;
      q->last->next = NULL;

      if (!q->first)
        q->first = q->last;
      return 1;
    } else {
      return 0;
    }

}

char *Queue_firstID(Queue *q)
{
    if (q && q->first)
        return q->first->id;
    else
        return NULL;
}

char *Queue_firstName(Queue *q)
{
    if (q && q->first)
        return q->first->name;
    else
        return NULL;
}

int Queue_dequeue(Queue *q)
{
    // implement this function
    if (q->first) {
      struct student *fst = q->first;
      struct student *nxt = fst->next;
      free(fst->name);
      free(fst);
      q->first = nxt;
      if (!q->first)
        q->last = NULL;
      return 1;
    } else {
      return 0;
    }
}

int Queue_drop(Queue *q, const char *id)
{
    // implement this function
    struct student *current = q->first;
    struct student *previous = NULL;

    while(current) {
      if (!strcmp(id, current->id)) {
        if(current == q->first) {
          q->first = current->next;
        } else if(current == q->last) {
          q->last = previous;
          q->last->next = NULL;
        } else {
          previous->next = current->next;
        }
        free(current->name);
        free(current);
        return 1;
      }
      previous = current;
      current = current->next;
    }
    return 0;
}

void Queue_delete(Queue *q)
{
    if (q) {
        while(Queue_dequeue(q));
        free(q);
    }
}
我无法找出内存泄漏的位置,因为我想我释放了出列和丢弃函数中的所有内存

提前感谢您。

在此代码中

int Queue_enqueue(Queue *q, const char *id, const char *name)
{
    // implement this function
    struct student *new = calloc(1, sizeof(struct student));
    if (strlen(id) <= 6) {
      strcpy(new->id, id);
      new->name = malloc(strlen(name) + 1);
      strcpy(new->name, name);
      new->name[strlen(name)] = '\0';
      if (q->last)
        q->last->next = new;
      q->last = new;
      q->last->next = NULL;

      if (!q->first)
        q->first = q->last;
      return 1;
    } else {
      return 0;
    }

}
也许你应该把它改成:

int Queue_enqueue(Queue *q, const char *id, const char *name)
{
    // implement this function
    struct student *new = calloc(1, sizeof(struct student));
    if (strlen(id) <= 6) {
        ....
    } else {
      free(new);   // Free the memory
      return 0;
    }

}
int Queue\u enqueue(队列*q,常量字符*id,常量字符*name)
{
//实现这个功能
struct student*new=calloc(1,sizeof(struct student));
本代码中的if(strlen(id)

int Queue_enqueue(Queue *q, const char *id, const char *name)
{
    // implement this function
    struct student *new = calloc(1, sizeof(struct student));
    if (strlen(id) <= 6) {
      strcpy(new->id, id);
      new->name = malloc(strlen(name) + 1);
      strcpy(new->name, name);
      new->name[strlen(name)] = '\0';
      if (q->last)
        q->last->next = new;
      q->last = new;
      q->last->next = NULL;

      if (!q->first)
        q->first = q->last;
      return 1;
    } else {
      return 0;
    }

}
也许你应该把它改成:

int Queue_enqueue(Queue *q, const char *id, const char *name)
{
    // implement this function
    struct student *new = calloc(1, sizeof(struct student));
    if (strlen(id) <= 6) {
        ....
    } else {
      free(new);   // Free the memory
      return 0;
    }

}
int Queue\u enqueue(队列*q,常量字符*id,常量字符*name)
{
//实现这个功能
struct student*new=calloc(1,sizeof(struct student));

if(strlen(id)在函数
Queue\u dequeue
中,如果
q->first
为空会发生什么?语句
struct student*nxt=fst->next;
失败。如果
q->first
为空,则不应运行
struct student*nxt=fst->next
行?我如何理解,如果
q->first
为空,则if语句为0接下来的行没有运行?我可能错了。在函数
Queue\u dequeue
中,如果
q->first
为空会发生什么?语句
struct student*nxt=fst->next;
失败。如果
q->first
为空,它就不应该运行
struct student*nxt=fst->next
行。我怎么理解如果
q->first
为空,则if语句为0,并且下一行未运行?我可能是错的。或者更好,在分配内存之前进行检查:
if(strlen(id)>6)返回0;struct student*new=calloc(1,sizeof(struct student));…
或者更好,在分配内存之前进行检查:
if(strlen(id)>6)返回0;struct student*new=calloc(1,sizeof(struct student));…
int Queue_enqueue(Queue *q, const char *id, const char *name)
{
    // implement this function
    struct student *new = calloc(1, sizeof(struct student));
    if (strlen(id) <= 6) {
        ....
    } else {
      free(new);   // Free the memory
      return 0;
    }

}