《C Primer Plus》第六版第十七章中的 film2.c 程序中的问题及其解决方案

本文分析了《CPrimerPlus》第六版第十七章中的film2.c程序出现的问题,详细解释了导致程序崩溃的原因,并提供了一种简单有效的解决方案。

前言(Introduction):

最近在通过模仿《C Primer Plus》中示例程序的方法复习C语言时,我发现《C Primer Plus》第六版第十七章中的 film2.c 程序运行出现问题。在查看错误反馈后,我找到了问题,并想出了一个解决方案。

Recently, I have imitated sample programs in the C Primer Plus to review the relevant knowledge of C language and Data Structure. I found that there is something wrong with the sample program named "film2.c" in the Ch 17 of the 6th edition of 《C Primer Plus》when I tried to run it. After having a look at the feedback, I found the problem and came up with a solution.

下面的链接是该程序的全部代码:

The link below is the code of this program: 

Ubuntu Pastebin

下面的代码块是出现问题的部分代码:

The code below is the code which has some problems:

    /* Program done, so free allocated memory */
    current = head;
    while (current != NULL)
    {
        free(current);
        current = current->next;
    }
    printf("Bye!\n");

问题(The Problem):

从控制台上可见,在该程序执行完对影片列表的输出后,并没有继续执行到最后的printf函数。从vscode上的反馈上可见,该程序执行到free函数时出现了问题,并停止运行。

From the console, we can see that after the program finished the output of this movie list, it didn't continue to run the last function of printf(). From the feedback on vscode, we can see that the program stopped running due to an error when executing the function of free().

原因(Cause):

    /* Program done, so free allocated memory */
    current = head;
    while (current != NULL)
    {
        free(current);
        current = current->next;
    }
    printf("Bye!\n");

在短暂的思考后,我发现了问题所在。该程序使用while循环来释放链表中的所有数据所占用的内存,使用了 current != NULL 作为while循环的执行条件,这一点没有问题。但是,在while循环内部,其先利用free()释放了current指针所指向的结构体,却又在下一句利用current调用已经被释放掉的结构体的成员变量。这里的 current->next 已经是一段无意义的值,所以程序发生了错误,停止在了这里。

After thinking for a while, I found the problem. This program wants to use while loop to release the memory which is used by the data in linked-list, and it uses "current != NULL" as a condition to execute the loop. It's OK and right. However, in the while loop, the program uses "free()" to release the memory which is pointed by the pointer named "current" first, and then use the "current->next" to execute the variable in the structure which has already been released. The "current->next" here has already been a meaningless value, so the program crashed and stopped.

解决方案(Solution):

那如何解决这个问题呢?

So how to solve this problem?

我认为可以创建一个临时的指针变量,在current被释放之前,保存current->next的值。

I think we can create a temp pointer to save the value of "current->next" before releasing the pointer named "current".

更改后的代码如下:

The changed code is as follows:

    puts("The program is done. Freeing the memory...");
    current = head;
    struct film * temp;
    while (current != NULL)
    {
        temp = current->next;
        free(current);
        current = temp;
    }
    puts("Byebye!");

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值