Thread: Function Free() does not delete the linked list?

  1. #1
    Registered User
    Join Date
    Jul 2012
    Location
    Ankara
    Posts
    52

    Question Function Free() does not delete the linked list?

    I created a program which was a link list in the only one function. Then I tried to delete the linked list with function free(), but the function couldn't do it, or I don't know how function free runs exactly.

    *First of all, I created an linked list, then print them.
    *Secondly I used to function free for deleting all of things.
    *Then, I encountered that the list was not removed exactly.Somethings were still there.

    If I deactive "startPtr=NULL;" with " // ", I can see that actually the linked list haven't been deleted from the memory.
    Note: The code of "startPtr=NULL;" whom I spoke is between "//Deleting Nodes" and "//Check out the list if deleted or not."

    So, Does function free perform the deletion? If you say yes, what is the situation the following?

    1.When active "startPtr=NULL;", the "//Check out the list if deleted or not." codes are deactive:
    FIGURE(1)
    Function Free() does not delete the linked list?-active-png



    2.But we see that when deactive "startPtr=NULL;", the "//Check out the list if deleted or not" codes aren't deactive. They runs, and they can show some items - such as grades,last names - from pseudo deleted list:
    FIGURE(2)
    Function Free() does not delete the linked list?-deactive-png

    To summurize my questions;
    a. Does function free() delete its argument from the memory?
    *If you say yes, why didn't free() delete some items as you can see Figure(2)?
    *If you say no, what do function free() do?

    b. Do startPtr=NULL just hide items apperantly?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int main()
    {
        struct gradeNode{
            char lastName[20];
            double grade;
            struct gradeNode *nextPtr;
        };
    
    
        typedef struct gradeNode GradeNode;
        typedef GradeNode *GradeNodePtr;
    
    
        GradeNodePtr startPtr=NULL;
        GradeNodePtr newPtr=NULL;
        GradeNodePtr previousPtr=NULL;
        GradeNodePtr currentPtr=NULL;
        GradeNodePtr tempPtr=NULL;
    
    
        //Node 1
        newPtr = malloc(sizeof(GradeNode));
        strcpy(newPtr->lastName,"Jones");
        newPtr->grade = 91.5;
        newPtr->nextPtr = NULL;
    
    
        startPtr = newPtr;
    
    
    
        //Node 2
        newPtr = malloc(sizeof(GradeNode));
        strcpy(newPtr->lastName,"Smith");
        newPtr->grade = 0.00;
        startPtr->nextPtr = newPtr;
    
    
    
    
        //Node 3
        newPtr = malloc(sizeof(GradeNode));
        strcpy(newPtr->lastName,"Adams");
        newPtr->grade = 85.0;
    
    
        previousPtr=NULL;
        currentPtr=startPtr;
    
    
        newPtr->nextPtr = currentPtr;
        startPtr = newPtr;
    
    
    
    
        //Node 4
        newPtr = malloc(sizeof(GradeNode));
        strcpy(newPtr->lastName,"Thompson");
        newPtr->grade = 73.5;
    
    
        previousPtr = (startPtr->nextPtr)->nextPtr;
        currentPtr=NULL;
    
    
        previousPtr->nextPtr = newPtr;
        newPtr->nextPtr = currentPtr;
    
    
    
    
    
    
        //Node 5
        newPtr = malloc(sizeof(GradeNode));
        strcpy(newPtr->lastName,"Pritchard");
        newPtr->grade = 66.5;
    
    
        previousPtr = startPtr->nextPtr;
        currentPtr = (startPtr->nextPtr)->nextPtr;
    
    
        previousPtr->nextPtr = newPtr;
        newPtr->nextPtr = currentPtr;
    
    
    
    
        //Printing the nodes.
        currentPtr = startPtr;
        while(currentPtr!=NULL){
            printf("Lastname = %s\nGrade = %6.2f\n\n",currentPtr->lastName,currentPtr->grade);
            currentPtr = currentPtr->nextPtr;
        }
    
    
        //Deleting the nodes.
        currentPtr = startPtr;
        while(currentPtr != NULL){
            tempPtr=currentPtr;
            currentPtr=currentPtr->nextPtr;
            free(tempPtr);
        }
        
    
        //startPtr=NULL;  //   LOOK AT HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!LOOK AT HERE?????????????????????????????????????LOOK AT HERE!!!!!!!!!!
    
    
    
        //Check out the list if deleted or not.
        printf("\nThe list was deleted?\n");
        currentPtr = startPtr;
        while(currentPtr!=NULL){
            printf("Lastname = %s\nGrade = %6.2f\n\n",currentPtr->lastName,currentPtr->grade);
            currentPtr = currentPtr->nextPtr;
        }
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by hefese
    a. Does function free() delete its argument from the memory?
    *If you say yes, why didn't free() delete some items as you can see Figure(2)?
    *If you say no, what do function free() do?
    Conceptually, free() deallocates the memory associated with its pointer argument, thus the memory is available to be allocated by say, a subsequent call of malloc(). However, what exactly happens is implementation defined, e.g., you may still be able to access that memory after the use of free() before calling malloc(), and it may well contain the exact same contents, or it could be zeroed. Or, maybe you will not be able to access the memory at all.

    Consequently, trying to determine if free() has "deleted" those items by examining the memory after calling free() tells you little, and in fact results in undefined behaviour.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I checked to see if the list is linked properly and found that it is.I also checked that the deletion of the nodes is as should be too!But then with the startPtr=NULL to comments the printing of the list behaves as the nodes were not free'ed!The first three nodes have garbage in the field name.

    now reading laserlights post i see that she was expecting so..

  4. #4
    Registered User
    Join Date
    Jul 2012
    Location
    Ankara
    Posts
    52
    Quote Originally Posted by laserlight View Post
    Conceptually, free() deallocates the memory associated with its pointer argument, thus the memory is available to be allocated by say, a subsequent call of malloc(). However, what exactly happens is implementation defined, e.g., you may still be able to access that memory after the use of free() before calling malloc(), and it may well contain the exact same contents, or it could be zeroed. Or, maybe you will not be able to access the memory at all.

    Consequently, trying to determine if free() has "deleted" those items by examining the memory after calling free() tells you little, and in fact results in undefined behaviour.
    I thought that function free() deletes structure's members and their values what 'argument in function free' points to completely, until I read you say. I understand that this was not like that. Thank you for these information.



    Quote Originally Posted by std10093 View Post
    I checked to see if the list is linked properly and found that it is.I also checked that the deletion of the nodes is as should be too!But then with the startPtr=NULL to comments the printing of the list behaves as the nodes were not free'ed!The first three nodes have garbage in the field name.

    now reading laserlights post i see that she was expecting so..
    Thank you for your interest.
    Last edited by hefese; 08-11-2012 at 07:29 AM.

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by hefese View Post
    I thought that function free() deletes structure's members what its argument points to completely, until I read you say. I understand that this was not like that. Thank you for these information.
    Thank you for your interest.
    Your welcome.However have in mind that if for example the field name was not declared statically but dynamically(the array had the length of the name that it would store),then first you should free the array name that was dynamically allocated(with malloc() perhaps) and then free the node.If you free the node you have no access to it's fields then.The fact that startPtr points at the start of your list here is not sure to happen everytime.In fact most times it points to garbage.So if you free your node,then you have no access to the array,and thus you can not deallocate it(with free).

  6. #6
    Registered User
    Join Date
    Jul 2012
    Location
    Ankara
    Posts
    52
    Quote Originally Posted by std10093 View Post
    Your welcome.However have in mind that if for example the field name was not declared statically but dynamically(the array had the length of the name that it would store),then first you should free the array name that was dynamically allocated(with malloc() perhaps) and then free the node.If you free the node you have no access to it's fields then.The fact that startPtr points at the start of your list here is not sure to happen everytime.In fact most times it points to garbage.So if you free your node,then you have no access to the array,and thus you can not deallocate it(with free).
    OK. Thank you again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Delete node in linked list..
    By aren34 in forum C Programming
    Replies: 3
    Last Post: 03-19-2011, 11:54 PM
  2. Delete all from linked list
    By lio in forum C Programming
    Replies: 4
    Last Post: 12-01-2010, 03:15 PM
  3. Delete Function in Doubly Linked List
    By Dampecram in forum C Programming
    Replies: 5
    Last Post: 11-15-2008, 04:30 PM
  4. delete node in Linked List
    By ronenk in forum C Programming
    Replies: 8
    Last Post: 01-05-2005, 01:28 AM
  5. why can't I delete the integers in my linked list?
    By LinkedListQ2 in forum C++ Programming
    Replies: 2
    Last Post: 01-06-2003, 02:43 PM

Tags for this Thread