Thread: Freeing the dynamic memory allocated in the Linked list

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    3

    Freeing the dynamic memory allocated in the Linked list

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    #include<stdlib.h>
    
    struct node
    {
        int data;
        int data2;
        struct node *link;
    };
    
    void addatbeg(struct node **q,int num,int num2)
    {
        struct node *temp;
    
        temp=(struct node *)malloc(sizeof(struct node));
        temp->data=num;
        temp->data2=num2;
        temp->link=*q;
        *q=temp;
        
    }
    
    void display (struct node *q)
    {
        while(q!=0)
        {
            printf("%d\t %d\t",q->data,q->data2);
            q=q->link;
        }
        printf("\n");
    }
    
    void main()
    {
      struct node *p;
      p=NULL;
    
      addatbeg(&p,123,43);
      display(p)  ;
      addatbeg(&p,97,47);
      display(p);
    
    }

    I want to ask what memory should I free ? The linked list or just the pointer p ?
    And after the execution of the program is done,ie I exit the IDE is the dynamically allocated memory still in use ? Same for the Pointer.

    I have not been able to figure out what to free, but I assume we use the free() function for it.

    Thanks

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Yes the free functions should be used.You should free what you have allocated dynamically(with malloc for example).So you have to free where the pointer points to .The pointer itself will be deallocated when the program is finished.
    I think that when the program terminates the dynamically allocated memory is going to be deallocated.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    All pointers returned by malloc must be passed to free to free the memory.
    The operating system will typically clean up your memory leaks when the program terminates, but don't rely on this: it is considered bad practice.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    To free the memory they point to!!! The pointers are not deallocated until the program is terminated.
    And keep in mind that if you have a large program and do not free your dynamically allocated memory then your heap may be become full!So it is your responsibility to deallocate memory when you do not need it ,in order to re-use it again

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    136
    When you are in main(), p is a pointer which is always pointing to the first(head) node. If you call free() for only p then only first node will be deallocated and not rest. It leads to memory leak. You can imagine a train without engine
    If you want to destroy the complete linked list then traverse each node to free().
    S_ccess is waiting for u. Go Ahead, put u there.

  6. #6
    Registered User
    Join Date
    Jul 2012
    Posts
    3
    Hey thanks everyone. I need some advice with coding.
    I've just started working with Data Structures using C. Should I do C++ first and then do data struct or do data structure and then C++.
    I'm confused & looking for what to do next after C. I did C last semester and I don't know any other prog lang. What should I try doing next ?
    Open to all suggestions

  7. #7
    Registered User
    Join Date
    Jan 2012
    Posts
    38
    i have the exact same ques as surajkaul w,r,t to coding. except i was considering Java instead of C++.....advice from the programming experts wud be really appreciated!!!

  8. #8
    Registered User
    Join Date
    Jul 2012
    Posts
    3
    Also any book recommendations for jumping into C++ from C & for Data Struct likewise.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Freeing dynamically allocated memory
    By kkk in forum C Programming
    Replies: 4
    Last Post: 05-30-2011, 12:24 PM
  2. Freeing dynamic allocated memory of structure
    By darekg11 in forum C Programming
    Replies: 14
    Last Post: 01-10-2011, 09:17 AM
  3. Freeing Dynamic allocated memory
    By TiNkiN in forum C Programming
    Replies: 10
    Last Post: 10-26-2010, 04:39 AM
  4. Error when freeing memory (allocated with realloc)
    By svdrjeug in forum C Programming
    Replies: 18
    Last Post: 01-03-2008, 11:16 AM
  5. freeing allocated memory
    By Devil Panther in forum C Programming
    Replies: 5
    Last Post: 06-29-2003, 01:43 PM

Tags for this Thread