Freeing a linked list -- possible bug.

This is a discussion on Freeing a linked list -- possible bug. within the C Programming forums, part of the General Programming Boards category; I've encountered the following code in a book I'm reading. It is given as an example for free()'ing a linked ...

  1. #1
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568

    Freeing a linked list -- possible bug.

    I've encountered the following code in a book I'm reading. It is given as an example for free()'ing a linked list. I believe that there is a bug in the highlighted part -- a freed memory location is being accessed.

    Am I right in thinking this, or have I misunderstood something?

    Code:
        current = head;
        while (current != NULL)
        {
            free(current);
            current = current->next;
        }
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Nope, you're right on the money... that is undefined behavior.

    Now... can you figure out how to fix it?

  3. #3
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568


    Already have (hopefully).
    Code:
        current = head;
        while (current != NULL)
        {
            temp = current->next;
            free(current);
            current = temp;
        }
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    There you go.

    However, you forgot to declare temp...
    Code:
      struct whatever temp;    // declare temp as struct type.
     current = head;
    
     while (current != NULL)
        {  temp = current->next;
            free(current);
            current = temp;
        }
    You know your lessons are sinking in when you find your teacher's mistakes
    Last edited by CommonTater; 01-25-2011 at 09:13 AM.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    However, you forgot to declare temp...
    Temp shouldn't be type struct whatever, it should be struct whatever *.

    I have a feeling that if msh is able to write code to delete a linked list, he knows how to declare his variables and probably left it out on purpose.

    Also, might be worth letting the authors of the book know about the mistake if you can. If it's a recent book then the mistake can be corrected in future editions.
    Last edited by DeadPlanet; 01-25-2011 at 10:04 AM.

  6. #6
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by DeadPlanet View Post
    Temp shouldn't be type struct whatever, it should be struct whatever *.

    I have a feeling that if msh is able to write code to delete a linked list, he knows how to declare his variables and probably left it out on purpose.

    Also, might be worth letting the authors of the book know about the mistake if you can. If it's a recent book then the mistake can be corrected in future editions.
    Thanks. It's indeed declared at the beginning of main(), along with the other variables.

    I'll definitely mail this to the authors. They have an errata for the previous edition up, but not for this.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 08:53 PM
  2. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 11:03 AM
  3. singly linked to doubly linked
    By jsbeckton in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 07:47 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21