malloc can';t为队列中的结构创建空间

malloc can';t为队列中的结构创建空间,c,struct,queue,malloc,C,Struct,Queue,Malloc,我试图创建一个包含2个int值的队列。这个问题发生在insert函数中。当我尝试为head->front->next程序分配内存时,程序停止。错误只发生在insert函数的else部分 struct Patient{ int national_id; int condition; }; struct Node{ struct Patient *info; struct Node *next; }; struct Queue{ int total; stru

我试图创建一个包含2个int值的队列。这个问题发生在insert函数中。当我尝试为head->front->next程序分配内存时,程序停止。错误只发生在insert函数的else部分

struct Patient{
   int national_id;
   int condition;
};

struct Node{
   struct Patient *info;
   struct Node *next;
};

struct Queue{
   int total;
   struct Node *rear;
   struct Node *front;
   int insert_number;
};

void insert (struct Queue *head, int natid, int cond);
void pop_min(struct Queue *head);
struct Queue *create_queue(void);
void destroy_queue(struct Queue *head);
void read_file(struct Queue *head);
void print_natid(struct Node *node);
void pop_all_elements(struct Queue *head);

void main(){
   struct Queue *head;

   head=create_queue();
   read_file(head);
   pop_all_elements(head);
   destroy_queue(head);
}

struct Queue *create_queue(void){

   struct Queue *head =(struct Queue*) malloc(sizeof(struct Queue));
   head->total=0;
   head->insert_number=0;
   return head;

}

void print_natid(struct Node *node){
   printf("%d ",node->info->national_id);
}


void insert (struct Queue *head,int natid, int cond){

   if(head->total==0){
      head->front=(struct Node*)malloc(sizeof(struct Node));
      head->front->info->national_id=natid;
      head->front->info->condition=cond;
      head->rear=head->front;
   }
   else{

      head->front->next=(struct Node*)malloc(sizeof(struct Node));
      head->front->next->info->national_id=natid;
      head->front->next->info->condition=cond;
      head->front=head->front->next;
   }


   head->insert_number++;
   head->total++;

   if(head->insert_number==3){
      pop_min(head);
      head->insert_number=0;
   }

   print_natid(head->rear);

}

void pop_min(struct Queue *head){

   printf("%d ",head->rear->info->national_id);

   struct Node *temp=head->rear;
   head->rear=head->rear->next;

   free(head->rear);
   free(temp);
}

void destroy_queue(struct Queue *head){
   free(head);
}

void pop_all_elements(struct Queue *head){
   struct Node *temp;

   while(head->rear!=head->front){
      print_natid(head->rear);
      temp=head->rear;
      free(temp);
      head->rear=head->rear->next;
   }

   print_natid(head->rear);
   free(head->rear);

}

void read_file(struct Queue *head){
   FILE *fp;
   int natid;
   int cond;
   fp=fopen("patients.txt","r");

   while (fscanf(fp,"%d %d", &natid, &cond) ==2)
      insert(head,natid,cond);

   fclose(fp);
}

我想你需要加上其他部分-

(head->front->next).info=(struct Patient *)malloc(sizeof(struct Patient));

我想你需要加上其他部分-

(head->front->next).info=(struct Patient *)malloc(sizeof(struct Patient));

我想你需要加上其他部分-

(head->front->next).info=(struct Patient *)malloc(sizeof(struct Patient));

我想你需要加上其他部分-

(head->front->next).info=(struct Patient *)malloc(sizeof(struct Patient));

我发现您的代码中存在以下问题:

问题1

insert()

您也没有在
insert
中设置新构造节点的
next
<代码>后->下一步
保持未初始化状态。如果您稍后访问该指针,您将遇到未定义的行为

我将更改以下代码块以修复上述问题并减少重复代码

您的代码:

if(head->total==0){
   head->front=(struct Node*)malloc(sizeof(struct Node));
   head->front->info->national_id=natid;
   head->front->info->condition=cond;
   head->rear=head->front;
}
else{

   head->front->next=(struct Node*)malloc(sizeof(struct Node));
   head->front->next->info->national_id=natid;
   head->front->next->info->condition=cond;
   head->front=head->front->next;
}
我的建议是:

struct Node* node = malloc(sizeof(struct Node));
node->next = NULL;
node->info = malloc(sizeof(struct Patient));
node->info->national_id=natid;
node->info->condition=cond;

if(head->total==0){
   head->front = node;
   head->rear = node;
}
else{
   head->front->next = node;
   head->front = node;
}
问题2

您在
pop\u min
中还有一个对
free
的额外呼叫

// This is wrong.
// Not only do you not need this but also it causes
// problems later when you try to use head->rear.
free(head->rear);
问题3

pop_all_元素
中的以下行是一个问题

  temp=head->rear;
  free(temp);

  // PROBLEM
  // Here you are trying to access memory that just got free'd in
  // previous line.
  head->rear=head->rear->next;
您需要交换最后两行。使用:

  temp=head->rear;
  head->rear=head->rear->next;
  free(temp);

我发现您的代码中存在以下问题:

问题1

insert()

您也没有在
insert
中设置新构造节点的
next
<代码>后->下一步
保持未初始化状态。如果您稍后访问该指针,您将遇到未定义的行为

我将更改以下代码块以修复上述问题并减少重复代码

您的代码:

if(head->total==0){
   head->front=(struct Node*)malloc(sizeof(struct Node));
   head->front->info->national_id=natid;
   head->front->info->condition=cond;
   head->rear=head->front;
}
else{

   head->front->next=(struct Node*)malloc(sizeof(struct Node));
   head->front->next->info->national_id=natid;
   head->front->next->info->condition=cond;
   head->front=head->front->next;
}
我的建议是:

struct Node* node = malloc(sizeof(struct Node));
node->next = NULL;
node->info = malloc(sizeof(struct Patient));
node->info->national_id=natid;
node->info->condition=cond;

if(head->total==0){
   head->front = node;
   head->rear = node;
}
else{
   head->front->next = node;
   head->front = node;
}
问题2

您在
pop\u min
中还有一个对
free
的额外呼叫

// This is wrong.
// Not only do you not need this but also it causes
// problems later when you try to use head->rear.
free(head->rear);
问题3

pop_all_元素
中的以下行是一个问题

  temp=head->rear;
  free(temp);

  // PROBLEM
  // Here you are trying to access memory that just got free'd in
  // previous line.
  head->rear=head->rear->next;
您需要交换最后两行。使用:

  temp=head->rear;
  head->rear=head->rear->next;
  free(temp);

我发现您的代码中存在以下问题:

问题1

insert()

您也没有在
insert
中设置新构造节点的
next
<代码>后->下一步
保持未初始化状态。如果您稍后访问该指针,您将遇到未定义的行为

我将更改以下代码块以修复上述问题并减少重复代码

您的代码:

if(head->total==0){
   head->front=(struct Node*)malloc(sizeof(struct Node));
   head->front->info->national_id=natid;
   head->front->info->condition=cond;
   head->rear=head->front;
}
else{

   head->front->next=(struct Node*)malloc(sizeof(struct Node));
   head->front->next->info->national_id=natid;
   head->front->next->info->condition=cond;
   head->front=head->front->next;
}
我的建议是:

struct Node* node = malloc(sizeof(struct Node));
node->next = NULL;
node->info = malloc(sizeof(struct Patient));
node->info->national_id=natid;
node->info->condition=cond;

if(head->total==0){
   head->front = node;
   head->rear = node;
}
else{
   head->front->next = node;
   head->front = node;
}
问题2

您在
pop\u min
中还有一个对
free
的额外呼叫

// This is wrong.
// Not only do you not need this but also it causes
// problems later when you try to use head->rear.
free(head->rear);
问题3

pop_all_元素
中的以下行是一个问题

  temp=head->rear;
  free(temp);

  // PROBLEM
  // Here you are trying to access memory that just got free'd in
  // previous line.
  head->rear=head->rear->next;
您需要交换最后两行。使用:

  temp=head->rear;
  head->rear=head->rear->next;
  free(temp);

我发现您的代码中存在以下问题:

问题1

insert()

您也没有在
insert
中设置新构造节点的
next
<代码>后->下一步
保持未初始化状态。如果您稍后访问该指针,您将遇到未定义的行为

我将更改以下代码块以修复上述问题并减少重复代码

您的代码:

if(head->total==0){
   head->front=(struct Node*)malloc(sizeof(struct Node));
   head->front->info->national_id=natid;
   head->front->info->condition=cond;
   head->rear=head->front;
}
else{

   head->front->next=(struct Node*)malloc(sizeof(struct Node));
   head->front->next->info->national_id=natid;
   head->front->next->info->condition=cond;
   head->front=head->front->next;
}
我的建议是:

struct Node* node = malloc(sizeof(struct Node));
node->next = NULL;
node->info = malloc(sizeof(struct Patient));
node->info->national_id=natid;
node->info->condition=cond;

if(head->total==0){
   head->front = node;
   head->rear = node;
}
else{
   head->front->next = node;
   head->front = node;
}
问题2

您在
pop\u min
中还有一个对
free
的额外呼叫

// This is wrong.
// Not only do you not need this but also it causes
// problems later when you try to use head->rear.
free(head->rear);
问题3

pop_all_元素
中的以下行是一个问题

  temp=head->rear;
  free(temp);

  // PROBLEM
  // Here you are trying to access memory that just got free'd in
  // previous line.
  head->rear=head->rear->next;
您需要交换最后两行。使用:

  temp=head->rear;
  head->rear=head->rear->next;
  free(temp);
当我尝试为head->front->next程序分配内存时,程序停止。错误只发生在insert函数的else部分

struct Patient{
   int national_id;
   int condition;
};

struct Node{
   struct Patient *info;
   struct Node *next;
};

struct Queue{
   int total;
   struct Node *rear;
   struct Node *front;
   int insert_number;
};

void insert (struct Queue *head, int natid, int cond);
void pop_min(struct Queue *head);
struct Queue *create_queue(void);
void destroy_queue(struct Queue *head);
void read_file(struct Queue *head);
void print_natid(struct Node *node);
void pop_all_elements(struct Queue *head);

void main(){
   struct Queue *head;

   head=create_queue();
   read_file(head);
   pop_all_elements(head);
   destroy_queue(head);
}

struct Queue *create_queue(void){

   struct Queue *head =(struct Queue*) malloc(sizeof(struct Queue));
   head->total=0;
   head->insert_number=0;
   return head;

}

void print_natid(struct Node *node){
   printf("%d ",node->info->national_id);
}


void insert (struct Queue *head,int natid, int cond){

   if(head->total==0){
      head->front=(struct Node*)malloc(sizeof(struct Node));
      head->front->info->national_id=natid;
      head->front->info->condition=cond;
      head->rear=head->front;
   }
   else{

      head->front->next=(struct Node*)malloc(sizeof(struct Node));
      head->front->next->info->national_id=natid;
      head->front->next->info->condition=cond;
      head->front=head->front->next;
   }


   head->insert_number++;
   head->total++;

   if(head->insert_number==3){
      pop_min(head);
      head->insert_number=0;
   }

   print_natid(head->rear);

}

void pop_min(struct Queue *head){

   printf("%d ",head->rear->info->national_id);

   struct Node *temp=head->rear;
   head->rear=head->rear->next;

   free(head->rear);
   free(temp);
}

void destroy_queue(struct Queue *head){
   free(head);
}

void pop_all_elements(struct Queue *head){
   struct Node *temp;

   while(head->rear!=head->front){
      print_natid(head->rear);
      temp=head->rear;
      free(temp);
      head->rear=head->rear->next;
   }

   print_natid(head->rear);
   free(head->rear);

}

void read_file(struct Queue *head){
   FILE *fp;
   int natid;
   int cond;
   fp=fopen("patients.txt","r");

   while (fscanf(fp,"%d %d", &natid, &cond) ==2)
      insert(head,natid,cond);

   fclose(fp);
}
如果事实上错误发生在
malloc()
内部,那么它是内存损坏问题的症状,其真正的轨迹可能在别处。一个候选者可能是来自
pop\u all\u elements()
的错误代码:

当它计算
head->rear->next
时,它会取消引用指向已释放内存的指针。通过将
free(temp)
移动到循环体的末端来修复此问题

另一个候选者是
pop\u min()

请注意,您不仅释放了旧的
,而且还释放了新的
,留下了一个无效的指针(如果程序继续运行,您很可能稍后会再次尝试释放该指针)。该函数有时由
insert()
调用,因此重复调用
insert()
可能会导致问题,例如由函数
read\u file()
执行的调用

其他候选代码包括出现在insert函数的两个分支中的以下代码:

    head->front->info->national_id=natid;
    head->front->info->condition=cond;
在这两种情况下,您都没有为
head->front->info
赋值,因此这两个赋值都会产生未定义的行为,这绝对可能表现为内存损坏。您可以考虑通过更改<代码>节点NO.FIX/C> >从<代码>结构>患者> <代码>到<代码>结构>患者< /代码>(在别处有相应的更改);除其他事项外,这还将使您无需在退出节点队列时释放那些动态分配的
患者

然而,另一种可能性是
malloc()
工作正常,但
head->front
是一个空指针或无效指针,因此分配给
head->front->next
会产生未定义的行为(表现为内存访问冲突和伴随的暂停)。我