0

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?

Community
  • 1
  • 1
some user
  • 1,693
  • 2
  • 14
  • 31
  • 2
    The free function doesn't and can't null the pointer variable. In order for it to be able to do that, you'd need to pass a pointer to the pointer variable. – Petr Skocik Jan 27 '17 at 20:03
  • @PSkocik thanks but what about value of head1? – some user Jan 27 '17 at 20:04
  • 1
    generally, make it a good habit, after `free`ing up a pointer, **always** set it to `NULL` afterwards, as if you ever de-reference a `NULL` pointer it will crash hinting what happened. – t0mm13b Jan 27 '17 at 20:05
  • 2
    Of course you print "Null" after the loop `while (head1) {...}`. The variable `head1` is the terminating linked list pointer, not a freed memory pointer supposedly reset. – Weather Vane Jan 27 '17 at 20:07
  • 1
    You ask why a pointer you don't change differs from a pointer you explicitly change to `NULL`? It is not clear what your problem is: Why is a pig not an eagle? – too honest for this site Jan 27 '17 at 20:12
  • @WeatherVane yeah thanks, I have got it now, head1 is nulled for traversing it . – some user Jan 27 '17 at 20:19
  • 1
    The value of a pointer becomes *indeterminate* when the object it points to reaches the end of its lifetime. Using the value of such a pointer (even by comparing it to `NULL`) is undefined behavior. – EOF Jan 27 '17 at 20:43
  • Related: http://stackoverflow.com/q/1879550/694576 – alk Jan 28 '17 at 07:21

6 Answers6

5

Freeing a pointer doesn't change its value.

You need to manually set it to NULL after freeing it if that's the behaviour you want.

James
  • 24,676
  • 13
  • 84
  • 130
4

When you free a pointer, you're not changing its value. You're just returning whatever memory it points to back to the pool.

In fact, given that free takes the value of a pointer instead of its address, that alone is enough to tell you that the value isn't changed, since there's no way to change it.

The reason head1 is NULL at the end of the loop is because you are modifying the value of head1 inside the loop to go down the list until it reaches the end. We know we're at the end of the list when we find a NULL value in the next pointer. So at the end of the loop, head1 is NULL.

dbush
  • 205,898
  • 23
  • 218
  • 273
2

When you allocate memory, the pointer naturally points to the beginning of the allocated block for you to reference. However, when you free a pointer, only the memory itself is freed.

The catch is that the pointer is still pointing to that location in memory that it was previously set to, even though the value of that block of memory is no longer useful.

Setting to NULL is not an automatic operation after freeing.

m_callens
  • 6,100
  • 8
  • 32
  • 54
2

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • That's perfect! Now I got this! Actually this head is nulled by traversing the linked list, not by freeing it. – some user Jan 27 '17 at 20:17
1

As mentioned above freeing a pointer has nothing to do with changing its value. It just tells the memory management that the related block (on the heap) can be reused.

This SO question provides further information: What is the difference between freeing the pointer and assigning it to NULL?

Community
  • 1
  • 1
dtell
  • 2,488
  • 1
  • 14
  • 29
0

free() doesn't mean destroy, it just does what it says, it makes memory space free. So after using free() the programmer is still free to use the pointer by reallocating memory space on to the heap with realloc().

Example:

int * ptr = (int*) calloc(1, sizeof(int));
*ptr = 100;
printf("ptr contains = %d\n", *ptr);
free(ptr);
ptr = (int*) realloc(ptr, (2 * sizeof(int)));
*ptr = 100;
*(ptr + 1) = 200;
printf("ptr = %d ptr+1 = %d\n", *ptr, *(ptr + 1));

you will set it to NULL only when you are really done with that pointer and you don't want it to be free but rather destroyed, inaccessible.

Andrés Sanchez
  • 99
  • 1
  • 10