Title: confused over the usage of free () in C
Tags: c;memory-management;linked-list
Question: This is done by someone who is better in C programming than me. 

confused over the usage of free () in C

below is the struct of linked list

```typedef struct node {
    int value;
    struct node *next;
} ListNode;

typedef struct list {
    ListNode *head;
} LinkedList;
```

after created a list with some nodes

his code does this when exit

```void deleteList(LinkedList *ll) {
    if (ll != NULL) {
        ListNode *temp;
        while (ll-&gt;head) {
            temp = ll-&gt;head;
            ll-&gt;head = temp-&gt;next;
            free(temp);
        }
        free(ll);
    }
}
```

The above is what I don't understand. Why he needs to create such complexity, why not just do free(ll). 
Please help 

thanks in advance.

Comment: @Deduplicator while your technical point is correct, I don't think it's busywork. Unless you malloc directly in main(), I don't think you can ever make a case where it won't eventually harm you... i.e. when you convert your standalone program to a lib or convert your simple program to a daemon, or convert your single-use program into one that processes multiple connections at once, etc. Please don't encourage bad practice for new programmers.

Comment: @Deduplicator... yeah, and I guess debugging future memory leaks doesn't count against that. To each his own. Enjoy!

Comment: Thanks everybody !!!! i was assuming free() will take the pointer and track down all the nodes and free all of them.

Comment: how well do you understand what a linked list is? there's a short explanation here or a long one...

Comment: Actually, every allocation operation should be reversed **unless you are tearing the house down and the platform will do it for you.** In the latter case it's busywork, though might be useful for leak-detection. Expanding on that theme, if you know your application will never in its whole lifetime (which must be short) allocate much memory, all deallocation can be deferred until exit. That also allows a more efficient implementation of the memory allocator.

Comment: @SanJacinto: Ever heard of future discounting? Making your program considerably faster and simpler and cheaper now, is worth much more than doing work for the nebulous future in which you might perhaps eventually under some conditions want to convert it to a demon or library.

Comment: You have to `free` every single thing you `malloc`.

Comment: Remember that each allocation operation should be reversed. Look at the implementation of adding nodes to the list - this should give you a clue.

Here is the accepted answer: Linked list is made up of individual objects that happen to point at each other. If you want to delete a list you have to delete all of its nodes. ```free()``` won't do that. It doesn't know that these objects make up a list. It doesn't even know that these objects contain pointers to anything. Therefore you need to iterate over the list and ```free``` each node by hand.

Comment for this answer: Thanks  !!!! i was assuming free() will take the pointer and track down all the nodes and free all of them.

Here is another answer: You can do with the following struct alone

```typedef struct node {
    int value;
    struct node *next;
} ListNode;
```

But every time you have to declare a global variable  ```struct node *HEAD```. In a bigger program it may confuse you. The author has done this so that you can create a linked list like a variable. Every time you have to create a new linked list all you have to do is declare

```LinkedList *ll;
```

When there are two struct, one has to free the objects of both the ```struct```. 


Here is another answer: That's because each pointer points to a memory location. You need to free all memory locations that were previously allocated. ```free(ll)``` would only remove the node pointed to by the ```ll``` pointer.


Here is another answer: if you have a linked list, suposse that every "*" is a node.

```        0  1  2  3  4
head--&gt; *--*--*--*--*
```

the first *, es the head, is you just do "free ll"
this will be on the memory

```        0   1  2  3  4
head--&gt;nul  *--*--*--*
```

the problem here, is, all the "memory" that you ask for those nodes will still be there, and now you can't know where is it (you have nothing poiting to that memory) for every malloc you need a free (not 100% true, but for simple things work).

What that algorithm do is:


get the reference to the next node (if you don't do this and you free the node, you won't be able to get the "next" node, becouse head will be pointing to nothing ). 
free the head.
make head point to the reference that you get before.



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 4, 'CommentCount': 8, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:04/12/14', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c><memory-management><linked-list>', 'Title': 'confused over the usage of free () in C', 'ViewCount': 142, 'id': 23031387, 'user_AboutMe': '', 'user_AccountId': 4179752.0, 'user_DisplayName': 'Steven Li', 'user_Reputation': 682.0, 'user_Views': 32.0, 'user_id': 3425131.0}}