当前位置:网站首页>An understanding of free() (an error in C Primer Plus)

An understanding of free() (an error in C Primer Plus)

2022-06-23 04:48:00 Boundless also initial heart

Looking at 《C Primer Plus( The fifth edition )》 The linked list part of , There is an example in the book , Knock it down , There will be an error prompt when running , I also think there is something wrong . I checked on the Internet again , There's something wrong with it , One about free() A bit of a hidden problem .
The code is as follows :

/* films2.c --  Use structure linked list */

#include "stdafx.h"

#include <stdio.h> 
#include <stdlib.h>
#include <string.h>
#define TSIZE 45

struct film{
    char title[TSIZE];
    int rating;
    struct film *next;
};

int main(int argc, char* argv[])
{
    struct film *head = NULL;
    struct film *prev, *current;
    char input[TSIZE];

    puts("Enter first movie title:");
    while(gets(input) != NULL && input[0] != '\0')
    {
        current = (struct film *) malloc(sizeof(struct film));
        if(head == NULL)
            head = current;
        else
            prev->next = current;
        current->next = NULL;
        strcpy(current->title, input);
        puts("Enter your rating <0-10>: ");
        scanf("%d", &current->rating);
        while(getchar() != '\n')
            continue;
        puts("Enter next movie title (empty line to stop): ");
        prev = 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 to complete , Free memory  */
    current = head;
    while (current != NULL)
    {
        free(current);
        current = current->next;
    }
    printf("Bye!\n");

    return 0;
}

The problem lies in the last part of freeing memory , There will be a prompt bug, Read the relevant instructions on the Internet , I think it makes sense , as follows

 One 、 If it has been released , So why can we point to the current structure next member ?
 The memory address is stored in the pointer variable , Free memory , Just handed over the right to use this address , And the value of the variable ( Address ) The system does not modify . What's released is memory , Is not a variable !!

 Two 、 After the memory is released , This memory , In fact, he handed over his authority , It can be used in , So as long as it is not in use , Data in this memory , Same as the previous deposit ?
 Yes , If the data is not rewritten in this memory , The data in this memory will not change .

 But there may be something wrong with your writing 
while (current != NULL)
{
    free(current); // Release first ,
    current = current->next; // Then use the memory , There will be BUG, There may be a program to occupy or change this memory at any time , And cause your program to run incorrectly !
}
 Write it correctly 
while (current != NULL)
{
    struct film *p=current ;
    current = current->next;

    free(p);
}

After the modification , Run correctly .
so , Use malloc(),free() It greatly increases the flexibility and convenience of the program , But be careful when using , Be careful .

原网站

版权声明
本文为[Boundless also initial heart]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230006250763.html