当前位置:网站首页>free( )的一个理解(《C Primer Plus》的一个错误)
free( )的一个理解(《C Primer Plus》的一个错误)
2022-06-23 03:51:00 【苍茫也初心】
在看《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", ¤t->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()大大增加了程序的灵活和方便,但是使用的时候要小心,再小心。
边栏推荐
- 智能语音时代到来,谁在定义新时代AI?
- 解决使用Exception抛出后,@Transactional不生效
- Abnova LiquidCell-负富集细胞分离和回收系统
- Mobile terminal city list sorting JS plug-in vertitylist js
- Distance measure - cosine distance
- What is metadata
- Cocos学习日记1——节点
- PTA:6-29 虚基类的应用-人与教师学生
- Pta:6-29 application of virtual base classes - people, teachers and students
- 离线数仓建模中常见的概念-术语
猜你喜欢
![[multimode] unimo](/img/a5/a857e20e1432ef3623527c8655a49a.png)
[multimode] unimo

Software development in 2022: five realities CIOs should know

x24Cxx系列EEPROM芯片C语言通用读写程序

ADR electronic transmission EDI solution of national adverse drug reaction monitoring center

距离度量 —— 余弦距离(Cosine Distance)

How does flutter achieve different zoom animation effects

Abnova酸性磷酸酶(小麦胚芽)说明书

Photoshop PS viewing pixel coordinates, pixel colors, pixel HSB colors
![[deep learning] deep learning reasoning framework tensorrt MNN openvino onnxruntime](/img/a9/11bc00a91b79358f28ada2d4c99f32.png)
[deep learning] deep learning reasoning framework tensorrt MNN openvino onnxruntime
![[pytoch] calculate the derivative of sin (x) by automatic differentiation](/img/a7/16dd9ecc13a986a9141ecc3fba00a1.png)
[pytoch] calculate the derivative of sin (x) by automatic differentiation
随机推荐
Abnova 血液总核酸纯化试剂盒方案
Volatile and threads
How to use shell script to monitor file changes
Monitoring artifact ZABBIX, from deployment to application, goes deep layer by layer
Second assignment notes
如何解决独立站多渠道客户沟通难题?这款跨境电商插件一定要知道!
PTA:6-71 时钟模拟
Photoshop PS viewing pixel coordinates, pixel colors, pixel HSB colors
win10查看my.ini路径
Online JSON to CSharp (c) class tool
Does the network disk also roll inside?
LabVIEW displays both hexadecimal and normal characters in the same table
论文阅读_关系抽取_CASREL
自动化测试常见的面试题
Cocos learning diary 1 - node
Please use the NLTK Downloader to obtain the resource
What is the open source database under Linux
MySQL导入大文件(可以是百万级,也可以是百级)
独立站聊天机器人有哪些类型?如何快速创建属于自己的免费聊天机器人?只需3秒钟就能搞定!
Abnova ACTN4纯化兔多克隆抗体说明书