D
disco
I am working on this example from a book "C Primer Plus" by Prata 4th
edition - p. 672. There is no erata on this problem at the publisher's
website.
1) Is it a violation of copyright laws to post example code from a
book to a newsgroup?
2) The program crashes as it tries to free memory and I would like to
know the best way to correct THIS section of the code. By assigning
current = head, current now points to the first structure in the
linked list. By freeing the first structure it frees all of the other
structures in the linked list below it. As a result, why isn't
current == NULL now ?
3) Is the author assigning current = current->next to make sure
everything gets freed? It seems unecessary.
Thanks,
disco
problem section:
/* Program is done, so free allocated memory */
current = head;
while (current != NULL)
{
free(current);
current = current->next;
}
printf("Bye!\n");
entire program:
/* Films2.c -- using a linked list of structures */
#include <stdio.h>
#include <stdlib.h> /* contains the malloc prototype */
#include <string.h> /* contains the strcpy prototype */
#define TSIZE 45 /* size of array to hold title */
struct film
{
char title[TSIZE];
int rating;
struct film *next; /* points to next struct in list */
};
int main (void)
{
struct film *head = NULL;
struct film *prev, *current;
char input[TSIZE];
printf("Enter first movie title:");
while (gets(input) !=NULL && input[0] != '\0')
{
current = (struct film*) malloc(sizeof(struct film));
if (head == NULL) /* first structure */
head = current;
else /* subsequent structures */
prev->next = current;
current->next = NULL;
strcpy(current->title, input);
printf("Enter your rating [0 to 10]: ");
scanf("%d", ¤t->rating);
while (getchar() != '\n')
continue;
printf("Please enter next movie title [enter to end]: ");
prev = current;
}
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;
}
/* Program is done, so free allocated memory */
current = head;
while (current != NULL)
{
free(current);
/* current = current->next; */
}
printf("Bye!\n");
return 0;
}
edition - p. 672. There is no erata on this problem at the publisher's
website.
1) Is it a violation of copyright laws to post example code from a
book to a newsgroup?
2) The program crashes as it tries to free memory and I would like to
know the best way to correct THIS section of the code. By assigning
current = head, current now points to the first structure in the
linked list. By freeing the first structure it frees all of the other
structures in the linked list below it. As a result, why isn't
current == NULL now ?
3) Is the author assigning current = current->next to make sure
everything gets freed? It seems unecessary.
Thanks,
disco
problem section:
/* Program is done, so free allocated memory */
current = head;
while (current != NULL)
{
free(current);
current = current->next;
}
printf("Bye!\n");
entire program:
/* Films2.c -- using a linked list of structures */
#include <stdio.h>
#include <stdlib.h> /* contains the malloc prototype */
#include <string.h> /* contains the strcpy prototype */
#define TSIZE 45 /* size of array to hold title */
struct film
{
char title[TSIZE];
int rating;
struct film *next; /* points to next struct in list */
};
int main (void)
{
struct film *head = NULL;
struct film *prev, *current;
char input[TSIZE];
printf("Enter first movie title:");
while (gets(input) !=NULL && input[0] != '\0')
{
current = (struct film*) malloc(sizeof(struct film));
if (head == NULL) /* first structure */
head = current;
else /* subsequent structures */
prev->next = current;
current->next = NULL;
strcpy(current->title, input);
printf("Enter your rating [0 to 10]: ");
scanf("%d", ¤t->rating);
while (getchar() != '\n')
continue;
printf("Please enter next movie title [enter to end]: ");
prev = current;
}
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;
}
/* Program is done, so free allocated memory */
current = head;
while (current != NULL)
{
free(current);
/* current = current->next; */
}
printf("Bye!\n");
return 0;
}