user-0 提问: 这个free是哪里有问题吗？两天了，没搞懂

回复:
user: user-1
content: //使用动态内存分为三步 //1.定义时，将指针为定义NULL //2.释放内存时，把指针变量重新赋值或者NULL //3.释放内存后，把指针变量赋值为NULL int main02() {     int *p = NULL;     p = (int *)malloc(4);          //这才是正确的写法     if (p != NULL){         free(p);//释放P所指向的内存空间,但指针变量p仍然留在栈中,成为了野指针         p = NULL;//释放野指针     }      return 0; }

user: user-2
content: if (!(*rear)->data)  //这里应该是  if ((*rear)->data) 吧。你那么写是为空的时候进入条件里。再去free，肯定有问题吧 { free((*rear)); *rear = NULL; }

user: user-0
content: 引用 3 楼 自信男孩 的回复:#include <stdio.h> #include <stdlib.h>  //#pragma once #define OK 1 #define ERROR 0 #define LEN sizeof(Qnode)  typedef struct QNode {     int data;     struct QNode *next; }Qnode, *QueuePtr;  typedef struct Queue {     Qnode *rear; }*LinkQueue;  //caculation.h: //#pragma once int InitQueue(LinkQueue *Q); QueuePtr CreateChain(LinkQueue Q,QueuePtr *rear,QueuePtr *tail); int InsertQueue(LinkQueue *Q); int DeleteElem(LinkQueue *Q); void Print(LinkQueue Q);  //main.c: //#include"stdio.h" //#include"typedef.h" //#include"caculation.h" //void main() int main() {     LinkQueue Q = NULL;     InitQueue(&Q);     printf("The Queue before operation:\n");     Print(Q);     InsertQueue(&Q);     printf("The Queue after Insert:\n");     Print(Q);     DeleteElem(&Q);     printf("The Queue after delete:\n");     Print(Q);     return 0; }  //caculation.c: //#include"stdio.h" //#include"stdlib.h" //#include"malloc.h" //#include"typedef.h" //#include"caculation.h" int InitQueue(LinkQueue *Q) {     QueuePtr tail = NULL;     *Q = (LinkQueue)malloc(sizeof(struct Queue));     CreateChain((*Q), &(*Q)->rear,&tail);     (*Q)->rear = tail;     return OK; } QueuePtr CreateChain(LinkQueue Q, QueuePtr *rear, QueuePtr *tail) {     *rear = (Qnode *)malloc(sizeof(LEN));     printf("Enter the data:");     scanf("%d", &(*rear)->data);     if (!(*rear)->data)     {         free((*rear));         *rear = NULL;     }     if (*rear)     {         if (!CreateChain(Q, &(*rear)->next, &(*tail)))         {             (*rear)->next = Q->rear;             (*tail) = (*rear);         }         return *tail;     }     else         return *rear; } int InsertQueue(LinkQueue *Q) {     int x = 0;     QueuePtr Position = NULL;     printf("Enter the data you wan insert the end:");     scanf("%d", &x);     Position = (Qnode *)malloc(sizeof(Qnode));     Position->data = x;     Position->next = (*Q)->rear->next;     (*Q)->rear->next = Position;     (*Q)->rear = Position;     return OK; } int DeleteElem(LinkQueue *Q) {     int data = 0;     QueuePtr pre = NULL, Position = NULL;     pre = (*Q)->rear;     Position = pre->next;     printf("Enter the elem you wan delete:");     scanf("%d", &data);      while (pre&&Position)     {         if (Position->data == data)         {             pre->next = Position->next;             if (Position == (*Q)->rear)                 (*Q)->rear = pre;             free(Position);             pre = Position = NULL;             continue;         }         Position = Position->next;         pre = pre->next;     }     return OK; } void Print(LinkQueue Q) {     QueuePtr Position = NULL;     Position = Q->rear->next;      while (1)     {         printf("%d\t", Position->data);         if (Position == Q->rear)             break;         Position = Position->next;     }     printf("\n");     return; } 亲测没发现问题～我用VC试了一下，发现删除的时候会报错，错误是DAMAGE:after normal block(#53)

user: user-0
content: 你的可以删除结点吗？   引用 6 楼 自信男孩 的回复:[quote=引用 5 楼 自信男孩 的回复:] [quote=引用 4 楼 D___H 的回复:] 删除结点功能的函数不能free 为什么不能free？[/quote] 我这边测试好像没发现问题，对比一下我发的代码～[/quote]

user: user-3
content: 引用 5 楼 自信男孩 的回复:[quote=引用 4 楼 D___H 的回复:] 删除结点功能的函数不能free 为什么不能free？[/quote] 我这边测试好像没发现问题，对比一下我发的代码～

user: user-3
content: 引用 4 楼 D___H 的回复:删除结点功能的函数不能free 为什么不能free？

user: user-0
content: 删除结点功能的函数不能free

user: user-3
content: #include <stdio.h> #include <stdlib.h>  //#pragma once #define OK 1 #define ERROR 0 #define LEN sizeof(Qnode)  typedef struct QNode {     int data;     struct QNode *next; }Qnode, *QueuePtr;  typedef struct Queue {     Qnode *rear; }*LinkQueue;  //caculation.h: //#pragma once int InitQueue(LinkQueue *Q); QueuePtr CreateChain(LinkQueue Q,QueuePtr *rear,QueuePtr *tail); int InsertQueue(LinkQueue *Q); int DeleteElem(LinkQueue *Q); void Print(LinkQueue Q);  //main.c: //#include"stdio.h" //#include"typedef.h" //#include"caculation.h" //void main() int main() {     LinkQueue Q = NULL;     InitQueue(&Q);     printf("The Queue before operation:\n");     Print(Q);     InsertQueue(&Q);     printf("The Queue after Insert:\n");     Print(Q);     DeleteElem(&Q);     printf("The Queue after delete:\n");     Print(Q);     return 0; }  //caculation.c: //#include"stdio.h" //#include"stdlib.h" //#include"malloc.h" //#include"typedef.h" //#include"caculation.h" int InitQueue(LinkQueue *Q) {     QueuePtr tail = NULL;     *Q = (LinkQueue)malloc(sizeof(struct Queue));     CreateChain((*Q), &(*Q)->rear,&tail);     (*Q)->rear = tail;     return OK; } QueuePtr CreateChain(LinkQueue Q, QueuePtr *rear, QueuePtr *tail) {     *rear = (Qnode *)malloc(sizeof(LEN));     printf("Enter the data:");     scanf("%d", &(*rear)->data);     if (!(*rear)->data)     {         free((*rear));         *rear = NULL;     }     if (*rear)     {         if (!CreateChain(Q, &(*rear)->next, &(*tail)))         {             (*rear)->next = Q->rear;             (*tail) = (*rear);         }         return *tail;     }     else         return *rear; } int InsertQueue(LinkQueue *Q) {     int x = 0;     QueuePtr Position = NULL;     printf("Enter the data you wan insert the end:");     scanf("%d", &x);     Position = (Qnode *)malloc(sizeof(Qnode));     Position->data = x;     Position->next = (*Q)->rear->next;     (*Q)->rear->next = Position;     (*Q)->rear = Position;     return OK; } int DeleteElem(LinkQueue *Q) {     int data = 0;     QueuePtr pre = NULL, Position = NULL;     pre = (*Q)->rear;     Position = pre->next;     printf("Enter the elem you wan delete:");     scanf("%d", &data);      while (pre&&Position)     {         if (Position->data == data)         {             pre->next = Position->next;             if (Position == (*Q)->rear)                 (*Q)->rear = pre;             free(Position);             pre = Position = NULL;             continue;         }         Position = Position->next;         pre = pre->next;     }     return OK; } void Print(LinkQueue Q) {     QueuePtr Position = NULL;     Position = Q->rear->next;      while (1)     {         printf("%d\t", Position->data);         if (Position == Q->rear)             break;         Position = Position->next;     }     printf("\n");     return; } 亲测没发现问题～