Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8160539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:16:25+00:00 2026-06-06T18:16:25+00:00

I am reading Chapter 17 of C Primer Plus, and here is the code

  • 0

I am reading Chapter 17 of C Primer Plus, and here is the code segament to free a linked list in the book:

/* Program done, so free allocated memory */
    current = head;
    while (current != NULL)
    {
        free(current);
        current = current->next;
    }
    printf("Bye!\n");
    return 0;

}

In the while statement, how could the “current” variable get the next value after already been freed? I searched for some code segaments to free linked lists on the web, and they seem to use two pointers in the while statement in order to avoid the previous problem.

However, if this is a bug, I couldn’t find this in errata. So any comments?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T18:16:27+00:00Added an answer on June 6, 2026 at 6:16 pm

    Yes, it’s clearly a bug. Accessing heap memory that has been free()d invokes undefined behavior. Bad book!

    The proper way is to buffer the next pointer before calling free():

    while (head != NULL)
    {
        struct list_node *next = head->next;
        free(head);
        head = next;
    }
    

    Notes:

    • No need to remember the old head of the list, so drop current and update head instead.
    • Define the buffering of the next node inside the loop for locality’s sake. Not sure about the proper type, don’t have the book (luckily!) and it’s not in the question.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading C Primer Plus these days and here is the code I
I am reading the book C++ Primer and at the file input output chapter
A chapter out of the book I have been reading has focused on memory
I am reading the APUE, Chapter 10. Here is my code. #include apue.h #include
I'm reading Eckel's book, IO chapter, and there is the following code (p. 667).
I am reading the chapter function templates of C++ Primer 3rd edition , and
I'm reading the book on Domain Driven Design of Eric Evans - Chapter 5
I am reading the book Think Python by Allen Downey. For chapter 4, one
I just finished reading the Functions chapter from Uncle Bob's Clean Code . The
I am reading the book -- Hadoop: The Definitive Guide In chapter 2 (Page

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.