I created a program which was a link list in the only one function. Then I tried to delete the linked list with function free(), but the function couldn't do it, or I don't know how function free runs exactly.
*First of all, I created an linked list, then print them.
*Secondly I used to function free for deleting all of things.
*Then, I encountered that the list was not removed exactly.Somethings were still there.
If I deactive "startPtr=NULL;" with " // ", I can see that actually the linked list haven't been deleted from the memory.
Note: The code of "startPtr=NULL;" whom I spoke is between "//Deleting Nodes" and "//Check out the list if deleted or not."
So, Does function free perform the deletion? If you say yes, what is the situation the following?
1.When active "startPtr=NULL;", the "//Check out the list if deleted or not." codes are deactive:
FIGURE(1)
2.But we see that when deactive "startPtr=NULL;", the "//Check out the list if deleted or not" codes aren't deactive. They runs, and they can show some items - such as grades,last names - from pseudo deleted list:
FIGURE(2)
To summurize my questions;
a. Does function free() delete its argument from the memory?
*If you say yes, why didn't free() delete some items as you can see Figure(2)?
*If you say no, what do function free() do?
b. Do startPtr=NULL just hide items apperantly?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
struct gradeNode{
char lastName[20];
double grade;
struct gradeNode *nextPtr;
};
typedef struct gradeNode GradeNode;
typedef GradeNode *GradeNodePtr;
GradeNodePtr startPtr=NULL;
GradeNodePtr newPtr=NULL;
GradeNodePtr previousPtr=NULL;
GradeNodePtr currentPtr=NULL;
GradeNodePtr tempPtr=NULL;
//Node 1
newPtr = malloc(sizeof(GradeNode));
strcpy(newPtr->lastName,"Jones");
newPtr->grade = 91.5;
newPtr->nextPtr = NULL;
startPtr = newPtr;
//Node 2
newPtr = malloc(sizeof(GradeNode));
strcpy(newPtr->lastName,"Smith");
newPtr->grade = 0.00;
startPtr->nextPtr = newPtr;
//Node 3
newPtr = malloc(sizeof(GradeNode));
strcpy(newPtr->lastName,"Adams");
newPtr->grade = 85.0;
previousPtr=NULL;
currentPtr=startPtr;
newPtr->nextPtr = currentPtr;
startPtr = newPtr;
//Node 4
newPtr = malloc(sizeof(GradeNode));
strcpy(newPtr->lastName,"Thompson");
newPtr->grade = 73.5;
previousPtr = (startPtr->nextPtr)->nextPtr;
currentPtr=NULL;
previousPtr->nextPtr = newPtr;
newPtr->nextPtr = currentPtr;
//Node 5
newPtr = malloc(sizeof(GradeNode));
strcpy(newPtr->lastName,"Pritchard");
newPtr->grade = 66.5;
previousPtr = startPtr->nextPtr;
currentPtr = (startPtr->nextPtr)->nextPtr;
previousPtr->nextPtr = newPtr;
newPtr->nextPtr = currentPtr;
//Printing the nodes.
currentPtr = startPtr;
while(currentPtr!=NULL){
printf("Lastname = %s\nGrade = %6.2f\n\n",currentPtr->lastName,currentPtr->grade);
currentPtr = currentPtr->nextPtr;
}
//Deleting the nodes.
currentPtr = startPtr;
while(currentPtr != NULL){
tempPtr=currentPtr;
currentPtr=currentPtr->nextPtr;
free(tempPtr);
}
//startPtr=NULL; // LOOK AT HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!LOOK AT HERE?????????????????????????????????????LOOK AT HERE!!!!!!!!!!
//Check out the list if deleted or not.
printf("\nThe list was deleted?\n");
currentPtr = startPtr;
while(currentPtr!=NULL){
printf("Lastname = %s\nGrade = %6.2f\n\n",currentPtr->lastName,currentPtr->grade);
currentPtr = currentPtr->nextPtr;
}
return 0;
}