"C Primer Plus" An error in the dynamic Link list deletion

Source: Internet
Author: User

"C Primer Plus" is really a great book, but the letter is not as good as the book, the author Stephen Prata may also hope that his readers can find a little bit of his inadvertently appear in the small mistake it!  in the fifth edition of the book, chapter 17, "Advanced Data Representation", listing 17.2 shows the following code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TSIZE 45
#define LEN sizeof (struct film)

struct film
{
Char Title[tsize];
int rating;
struct film *next;
};

int main ()
{
struct film *head = NULL;
struct film *pre, *current, *hou;
Char Input[tsize];

/*Collect and store information*/
Puts ("Enter first movie title:");
while (gets (input) = NULL && input[0]! = '% ')
{
Current = (struct film*) malloc (LEN);
if (head = = NULL)
{
head = current;
}
Else
{
Pre->next = current;
}
Current->next = NULL;
strcpy (current->title, input);
Puts ("Enter your Rating<0-10>:");
scanf ("%d", ¤t->rating);
while (GetChar ()! = ' \ n ')
Continue
Puts ("Enter next movie title (empty line to stop):");
Pre = current;
}

/*give a list of movies*/
if (head = = NULL)
{
printf ("No data entered.");
}
Else
{
printf ("Here is the movie list:\n");
}
current = head;
while (current! = NULL)
{
printf ("movie:%s rating:%d\n", Current->title, current->rating);
Current = current->next;
}

/* task is completed, so the allocated space is freed */
current = head;
while (head! = NULL)
{
Free (current);
Current = current->next;
}


printf ("bye!\n");

return 0;
}
           an assertion failure is raised in the vc++6.0, the problem is found in the last part after debugging, and the "task is completed, so the allocated memory space portion is freed" (bold in the text), after debugging, the following problems are found:
This section starts with current pointing to the Head,while Loop first free (current), since the current memory space has been freed, the next step how to make current point to Current->next? Therefore, an assertion failure occurs.
Modification method: Use another pointer hold pointing to the node that holds the pointer to the next node that is currently releasing the node.

/*
The task is completed, so the allocated memory is freed */
current = head;
while (current! = NULL)
{
Hold = current->next;
Free (current);
current = hold;
}



add: Then in the book's program listing 17.5 validates my idea, the author gives a clear list of functions, the basic idea is consistent, the code is as follows:

     /*released bymalloc ()Allocated Memory*/
/*position the list pointer tonull*/
void Emptythelist (List *plist)
{
node* Psave;
while (*plist! = NULL)
{
Psave = (*plist)->next;
Free (*plist);
*plist = Psave;
}     }

"C Primer Plus" An error in the dynamic Link list deletion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.