Here are my problems:
I have used P - freeAid pointed to the next node P - free, I also see in the debug, P - free next node address==P - freeAid value,
But I try to use p - freeaid to adjust the next node of p - free but the value of num display system error, rather odd,
(2) when I use the free released after referring to p - free memory space, I use the p - free to read the value of the inside is still have the result, but display in the inside of the debug p free value is "- 179469979 (similar to the number of)" I feel this is proof that space has been released, but still can read value, this is super,
Code on the below, please the great god solve, gratitude can't help ~
typedef struct student_link
{
Char name [20].
int num;
Int score;
Struct student_link * next;
} STU.
Void creat_head (STU * * p_head, STU * p_new) {
STU * p_mov;
P_mov=* p_head;
If (* p_head==NULL) {
* p_head=p_new;
P_new - & gt; Next=NULL;
}
The else {
While ((p_mov - & gt; Next)!=NULL) {
P_mov=p_mov - & gt; Next;
}
P_mov - & gt; Next=p_new;
P_new - & gt; Next=NULL;
}
}
Void showThe_link (STU * p_head) {
STU * p_mov;
P_mov=p_head;
If (p_head==NULL) {
Printf (" the current list is empty \ n ");
return;
} else {
While (p_mov!=NULL) {
Printf (" % d % d % s \ n ", p_mov - & gt; Num, p_mov - & gt; Score, p_mov - & gt; Name);
P_mov=p_mov - & gt; Next;
}
}
}
STU * creat_node (int num, int score, char stu_name []) {
STU * p_new;
P_new=(STU *) malloc (sizeof (STU));
Strcpy (p_new - & gt; The name, stu_name);
P_new - & gt; Num=num;
P_new - & gt; Score=score;
Return p_new;
}
Void add_node (STU * * p_head, int num, int score, char stu_name []) {
Creat_head (p_head creat_node (num, score, stu_name));
}
* * p_head void free_node (STU) {
STU * p_free;
STU * p_freeAid;
Printf (" p_freeAid values are: 0 x % x \ n ", p_freeAid - & gt; Num);
P_free=* p_head;
If (* p_head==NULL) {
Printf (" the list is empty, don't need to release \ n ");
}
The else {
While (p_free!=NULL) {
P_freeAid=p_free - & gt; Next;
Printf (" p_freeAid values are: 0 x % x \ n ", p_free - & gt; Next - & gt; Num);
Printf (" p_freeAid values are: 0 x % x \ n ", p_freeAid);
Printf (" p_free values are: 0 x % x \ n ", p_free - & gt; Next);
Int num=p_free - & gt; Num.
Printf (" % d \ n ", num);
Free (p_free);
Printf (" % d \ n ", p_free - & gt; Num);
P_free=p_freeAid;
}
}
//* p_head=NULL;
}
Int main ()
{
STU * link_head=NULL;
/* int num, I;
Printf (" please enter the list of initial number: \ n ");
The scanf (" % d ", & amp; Num);
for(i=0; iP_new=(STU *) malloc (sizeof (STU));
Printf (" please enter the student id, scores, name: \ n ");
The scanf (" % d % d % s ", & amp; P_new - & gt; Num, & amp; P_new - & gt; Score, & amp; P_new - & gt; Name);
} */
Char STR [12]="cherry";
Add_node (& amp; Link_head, 6, 7, STR);
Add_node (& amp; Link_head, 9, 7, STR);
ShowThe_link (link_head);
//free_node (& amp; Link_head);
ShowThe_link (link_head);
return 0;
}
CodePudding user response:
There are several problems in free_node function,1. Printf (" p_freeAid values are: 0 x % x \ n ", p_freeAid - & gt; Num); P_freeAid before it is wild pointer, such operations could lead to a period of error;
(2) printf (" p_freeAid values are: 0 x % x \ n ", p_free - & gt; Next - & gt; Num); The print may also appear many mistakes, because p_free is not NULL, is not equal to p_free - & gt; Next is not NULL;
3. The free (p_free);
Printf (" % d \ n ", p_free - & gt; Num);
Print this statement after the release, the result is undefined, because space has been released, its content is uncertain, release the space cannot be used directly again, need to malloc;
4.//* p_head=NULL; It cannot comment out, has been released, because the head nodes in order to ensure that use behind rival node does not appear problem, it need to add (remove comments);
void free_node (STU * * p_head)
{
STU * p_free;
STU * p_freeAid;
//printf (" p_freeAid values are: 0 x % x \ n ", p_freeAid - & gt; Num);
P_free=* p_head;
If (* p_head==NULL) {
Printf (" the list is empty, don't need to release \ n ");
return;
}
//else {
While (p_free!=NULL) {
P_freeAid=p_free - & gt; Next;
//printf (" p_freeAid values are: 0 x % x \ n ", p_free - & gt; Next - & gt; Num);
Printf (" p_freeAid values are: 0 x % x \ n ", p_freeAid);
Printf (" p_free values are: 0 x % x \ n ", p_free - & gt; Next);
Int num=p_free - & gt; Num.
Printf (" % d \ n ", num);
Free (p_free);
Printf (" % d \ n ", p_free - & gt; Num);
P_free=p_freeAid;
}
//}
* p_head=NULL;
}
Code logic can be a little change, remove the else,
In addition, print pointer suggest using % p
CodePudding user response:
Hello, first of all thank you very much for your answer, I am beginner c, using Java simulation list before, but if not c language to consider so much, about you said I still have some questions,"Printf (" p_freeAid values are: 0 x % x \ n", p_freeAid - & gt; Num); P_freeAid before it is wild pointer, this could lead to a period of operation error "
About this sentence, P - freeAid point, I think it is pointing in the direction of P - free - & gt; Next, and the execution of this line of code, clearly in free. The next inside still have value, so according to my understanding of P - freeAid inside should be have a value, and I in the debug, P - freeAid inside of "num" also have value, but I'm using the printf function callback when process is not bottom go to, so I don't understand, so I hope you can give a little bit about that solve,
And before that showLink function inside, I take p_head - & gt; Next the value assigned to the p_mov, and then when I was in print is called p_mov - & gt; Num to values,
I use p_freeAid in free function to receive the p_free - & gt; The value of the next, then also want to call p_freeAid - & gt; Num value to why not?
To understand,,,,