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

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

/* films2.c  -- 使用结构链表*/

#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;
    }

    /*  给出电影列表  */
    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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值