Click here to Skip to main content
15,898,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Quote:
When to use the free() method in C. Below I have stated a Linkedlist , the program is fine evn without releasing the memory here ? What is happening here ?
When should I use the free() method or release the memory from the heap ?

C++
#include "stdio.h"
#include "stdlib.h"

struct Node {
    int data;
    struct Node *next;
};

struct Node *head = NULL;

void insert(int data) {
    
    struct Node *newNode = malloc(sizeof(struct Node));
    
    if (head == NULL) {
        head = newNode;
        head->data = data;
        head->next = NULL;
    } 
    else {
        
        struct Node *temp = head;
        while (temp->next != NULL) {
            temp = temp->next;
        }
        temp->next = newNode;
        newNode->data = data;
        newNode->next = NULL;
    }
}

void display() {
    
    if (head == NULL) {
        
        printf("LIST IS EMPTY!");
    } else {
        struct Node *temp = head;
        while (temp != NULL) {
            printf("%d\n", temp->data);
            temp = temp->next;
        }
    }
}

int main() {
    insert(11);
    insert(12);
    insert(13);
    insert(14);
    display();
}


What I have tried:

tried google and other online resources
Posted
Updated 16-Oct-20 7:15am
v2

You should release the memory you have allocated when you are finished using the data it contains. For your program that would presumably be after display() is called.
 
Share this answer
 
You use free when you no longer need the block of memory. So, for example, you might free the memory used by a single node after you delete it from the list. In small programs it is rarely a problem, and the heap gets destroyed when the program terminates. But in larger applications it can be critical.
 
Share this answer
 
As per the first two answers, you free the memory when you are through with it.

The trick is to know when you're through with it.

If it's in main(), then free it (at least) before the end of the program (exit of some sort).

If you're inside of a function, then release it before you return from the function. This has two caveats:
1) if you are passing the pointer out of the function then you'll need to free it in the caller when, as always, you are done using it.
2) if your compiler supports alloca(), or something like it, then you use that to assign memory - this is automatically released when you return from the function call. (I think this is because it's stored on the stack instead of the heap, but I'm not sure about this point).

As I noted at the top - you need to know when you're done with the memory - not releasing it (i.e., free() ), say, for example, in a function, will keep grabbing new memory and this may end up causing your application to crash.
 
Share this answer
 
Quote:
When to use the free() method in C. Below I have stated a Linkedlist , the program is fine evn without releasing the memory here ? What is happening here ?
When the process exits the OS automatically releases all the memory the process allocated.


Quote:
When should I use the free() method or release the memory from the heap ?
When you no longer need it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900