user-0 提问: C语言使用free报错

回复:
user: user-1
content: 崩溃的时候在弹出的对话框按相应按钮进入调试，按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处，看不懂时双击下一行，直到能看懂为止。 仅供参考：//带表头结点的单向链表 #include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <time.h> struct NODE {     int          data;     struct NODE *next; } H,*head,*p,*q,*s1,*s2,*s3,*s4,*s; int i,j,k,n,t,m; int main() {     srand(time(NULL));      //填写头节点数据     H.data=-1;     H.next=NULL;     head=&H;      //创建10个节点的单链表     p=head;     for (i=0;i<10;i++) {         q=(struct NODE *)malloc(sizeof(struct NODE));         if (NULL==q) return 1;         q->data=rand()%100;//填写0..99的随机值         q->next=NULL;         p->next=q;         p=q;     }      //输出整个单链表     s=head->next;     while (1) {         if (NULL==s) {             printf("\n");             break;         }         printf("%02d->",s->data);         s=s->next;     }      //将值为5的结点插入到单链表的第k个结点前     k=3;     n=0;     p=head;     while (1) {         if (NULL==p) {             break;         }         n++;         if (k==n) {             q=(struct NODE *)malloc(sizeof(struct NODE));             if (NULL==q) return 1;             q->data=5;             q->next=p->next;             p->next=q;             break;         }         p=p->next;     }      //输出整个单链表     s=head->next;     while (1) {         if (NULL==s) {             printf("\n");             break;         }         printf("%02d->",s->data);         s=s->next;     }      //删除第k个节点     k=5;     n=0;     p=head;     while (1) {         if (NULL==p) {             break;         }         n++;         if (k==n) {             q=p->next;             if (q) {                 p->next=q->next;                 free(q);             }             break;         }         p=p->next;     }      //输出整个单链表     s=head->next;     while (1) {         if (NULL==s) {             printf("\n");             break;         }         printf("%02d->",s->data);         s=s->next;     }      //从小到大排序     for (p=head;p!=NULL && p->next!=NULL;p=p->next) {         for (q=p->next;q!=NULL && q->next!=NULL;q=q->next) {             if (p->next->data > q->next->data) {                  //交换data //              printf("swap %02d %02d\n",p->next->data,q->next->data); //              t=p->next->data;p->next->data=q->next->data;q->next->data=t;                  //或者                  //交换next //              printf("swap %02d %02d\n",p->next->data,q->next->data);                 s1=p->next;                 s2=p->next->next;                 s3=q->next;                 s4=q->next->next;                  if (s2!=s3) {                      p->next=s3;                     s3->next=s2;                      q->next=s1;                     s1->next=s4;                 } else {                      p->next=s3;                     s3->next=s1;                            q=s3;                     s1->next=s4;                 }                  //输出整个单链表 //              s=head->next; //              while (1) { //                  if (NULL==s) { //                      printf("\n"); //                      break; //                  } //                  printf("%02d->",s->data); //                  s=s->next; //              } //              getchar();             }         }     }      //输出整个单链表     s=head->next;     while (1) {         if (NULL==s) {             printf("\n");             break;         }         printf("%02d->",s->data);         s=s->next;     }      //将单链表中前 m 个结点和后 n 个结点进行互换,m+n为链表总长10     m=4;     n=6;     k=0;     p=head;     while (1) {         if (NULL==p) {             break;         }         k++;         if (m+1==k) {             q=p;         }         s=p;         p=p->next;     }     s1=head->next;     head->next=q->next;     s->next=s1;     q->next=NULL;      //输出整个单链表     s=head->next;     while (1) {         if (NULL==s) {             printf("\n");             break;         }         printf("%02d->",s->data);         s=s->next;     }      //释放所有节点     p=head->next;     while (1) {         if (NULL==p) {             break;         }         q=p->next;         free(p);         p=q;     }      return 0; } //18->94->58->17->27->20->43->57->75->78-> //18->94->05->58->17->27->20->43->57->75->78-> //18->94->05->58->27->20->43->57->75->78-> //05->18->20->27->43->57->58->75->78->94-> //43->57->58->75->78->94->05->18->20->27-> //

user: user-2
content: free失败一般是 1.free不是malloc的内存 2.双重free，释放已经释放的内存

user: user-3
content: /*把线程数组加入到队列中*/ void addQueue() { 	ready->head = ready->tail = NULL; 	for (int i = 0; i < count; i++) { 		if (i == 0) { 			ready->head = &pcbs[i]; 			pcbs[0].next = &pcbs[1];   //加一句 		} 		else if (i == count - 1) { 			ready->tail = &pcbs[count - 1]; 		}  		if (i < count - 1) { 			pcbs[i].next = &pcbs[i + 1]; 		} 	} }

user: user-3
content: /*对队列排序*/ void sort() { 	//何用？ 	//for (int i = 0; i<count; i++) { 	//	if (i == 0) { 	//		pcbs[i] = *ready->head; 	//	} 	//	else { 	//		pcbs[i] = *pcbs[i - 1].next;   // 	//	}  	//}  	for (int i = 0; i < count; i++) { 		for (int j = count - 1; j > i; j--) { 			if (pcbs[j].priority > pcbs[j - 1].priority) { 				PCB temp; 				temp = pcbs[j]; 				pcbs[j] = pcbs[j - 1]; 				pcbs[j - 1] = temp; 			} 		} 	} 	addQueue();//对拍好序的数组加入到队列 }

user: user-3
content: 部分修改：  /*删除线程操作*/ void delete() { 	PCB *pb, *pre; 	pre = NULL; 	pb = ready->head; 	while (pb && pb->needTime) 	{ 		pre = pb; 		pb = pb->next; 	} 	if (NULL == pre) 	{ 		//free(pb); 		ready->head = ready->tail = NULL; 	} 	else 	{ 		pre->next = pb->next; 	} 	free(pb); 	//ready->head = pb->next; 	//	free(pb); 	count--; 	//if (ready->head == NULL) { 	//	ready->tail = ready->head; 	//} }  //运行 void priorityRun(int n) { 	priorityCreate(n); 	initReady(); 	addQueue(); 	sort(); 	while (count != 0) { 		PCB *pcb = ready->head;//取出队列头部的 		pcb->needTime--; 		pcb->priority = pcb->priority - 3; 		printf("队列%s	  进程剩余需要的时间为%d	优先级剩余为%d \n", pcb->name, pcb->needTime, pcb->priority); 		if (pcb->needTime == 0) { 			delete(); 		} 		//else { 			sort();   //只要有线程的优先级发生变化，就要排序 		//} 	} }

user: user-0
content: 引用 5 楼 zhangxiangDavaid 的回复: /*对队列排序*/ void sort() { 	//何用？ 	//for (int i = 0; i<count; i++) { 	//	if (i == 0) { 	//		pcbs[i] = *ready->head; 	//	} 	//	else { 	//		pcbs[i] = *pcbs[i - 1].next;   // 	//	}  	//}  	for (int i = 0; i < count; i++) { 		for (int j = count - 1; j > i; j--) { 			if (pcbs[j].priority > pcbs[j - 1].priority) { 				PCB temp; 				temp = pcbs[j]; 				pcbs[j] = pcbs[j - 1]; 				pcbs[j - 1] = temp; 			} 		} 	} 	addQueue();//对拍好序的数组加入到队列 }  如果把线程删除以后，数组中的元素就改变了，所以把队列中的线程再给数组赋一遍，保证数组中没有删除的那个线程

user: user-0
content: 引用 5 楼 zhangxiangDavaid 的回复: /*对队列排序*/ void sort() { 	//何用？ 	//for (int i = 0; i<count; i++) { 	//	if (i == 0) { 	//		pcbs[i] = *ready->head; 	//	} 	//	else { 	//		pcbs[i] = *pcbs[i - 1].next;   // 	//	}  	//}  	for (int i = 0; i < count; i++) { 		for (int j = count - 1; j > i; j--) { 			if (pcbs[j].priority > pcbs[j - 1].priority) { 				PCB temp; 				temp = pcbs[j]; 				pcbs[j] = pcbs[j - 1]; 				pcbs[j - 1] = temp; 			} 		} 	} 	addQueue();//对拍好序的数组加入到队列 }  如果把线程删除以后，数组中的元素就改变了，所以把队列中的线程再给数组赋一遍，保证数组中没有删除的那个线程

user: user-4
content: /*按照优先权法创建线程*/ void priorityCreate(int n) {     char name[5];     PCB *p;     printf("请输入进程每个进程的名字：\n");     for (int i = 0; i < n; i++) {         gets_s(name,5);         p = (PCB *)malloc(sizeof(PCB));         strcpy(p->name, name);         p->priority = rand() % 10 + 10;         p->needTime = rand() % 5 + 5;         p->next = NULL;           pcbs[i] = *p;         count++;     }   }

user: user-3
content: 点击中止，alt+7打开调用堆栈，双击查看，进行调试

user: user-0