Does the value of pointer become NULL after freeing it?
int* p = malloc(sizeof(*p));
free(p);
if(p==NULL)
printf("Null\n");
else
printf("Not null\n");
Output:
Not null
Well, I assume not;
Anyway, I have asked a question earlier today :
Check it out here: C - How can I free dynamically allocated memory?
List* head1 = NULL;
insertFront(&head1, 1);
insertFront(&head1, 2);
print(head1);
while (head1)
{
List *temp = head1;
head1 = head1->next;
free(temp);
}
if(head1 == NULL)
printf("Null\n");
else
printf("Not null\n");
Output in this case:
Null
In this case after freeing head1 (nodes also) the head1 becomes null, does'nt it?
And finally, am I missing some concepts?
head1 is null, however p isn't.
My Question is:
Why values differs between head1 and p?
The condition of the loop
while (head1)
^^^^^^
{
List *temp = head1;
head1 = head1->next;
free(temp);
}
becomes equal to false
when the data member next
of the next node is equal to NULL
head1 = head1->next;
^^^^^ ^^^^^
This has nothing common with the function free
. The function accepts the argument by value. That is it deals with a copy of the original [pointer. So the original pointer itself will not be changed.