free( )的一个理解(《C Primer Plus》的一个错误)

来源:互联网 发布:e25歼击车数据 编辑:程序博客网 时间:2024/05/16 00:37

在看《C Primer Plus(第五版)》的链表部分时,书上有个例子,敲下来,运行会有错误提示,我也觉得有点问题。又在网上查了下,确实有点问题,一个关于free()的有点隐蔽的问题。
代码如下:

/* films2.c  -- 使用结构链表*/#include "stdafx.h"#include <stdio.h> #include <stdlib.h>#include <string.h>#define TSIZE 45struct 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;    }    /*  给出电影列表  */    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;    }    /* 任务完成,释放内存 */    current = head;    while (current != NULL)    {        free(current);        current = current->next;    }    printf("Bye!\n");    return 0;}

问题就出在最后释放内存的部分,会提示有bug,查阅了网上的相关说明,觉得言之有理,如下

一、如果已经释放,那么为啥还能指到当前结构的next成员?指针变量中存储的是内存地址,释放内存,只是将这块地址的使用权交出去了,而变量的值(地址)系统并不进行修改。释放的是内存,不是变量!!二、那么释放该内存后,这块内存,实际是把权限交出了,可以在使用了,那么只要不在使用,该内存的数据,和先前存入的一样?是的,如果没有在这块内存中重新写入数据,这块内存中的数据是不会发生变化的。但是你的写法有可能会出问题while (current != NULL){    free(current); //先释放,    current = current->next; //再使用该内存,会有BUG,随时都可能会有程序去占用或更改这块内存,而造成你的程序运行出错!}正确写法while (current != NULL){    struct film *p=current ;    current = current->next;    free(p);}

修改之后,正确运行。
可见,使用malloc(),free()大大增加了程序的灵活和方便,但是使用的时候要小心,再小心。

1 0
原创粉丝点击