当前位置:网站首页>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", ¤t->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 .
边栏推荐
- Audio and video development journey (63) - animation and rendering of Lottie source code analysis
- X24cxx series EEPROM chip C language universal reading and writing program
- 开关磁阻电机悬浮驱动IR2128小结
- 离线数仓建模中常见的概念-术语
- STL教程3-异常机制
- 32单片机一个变量多个.c里使用
- Abnova ACTN4纯化兔多克隆抗体说明书
- What are the characteristics of SRM supplier management system developed by manufacturing enterprises
- 395. 冗余路径
- 反编译
猜你喜欢

语料库数据处理个案实例(分词和分句、词频统计、排序)

win10下安装、运行MongoDB

语料库数据处理个案实例(词性赋码、词性还原)

Examples of corpus data processing cases (part of speech encoding, part of speech restoration)

VGG 中草药识别

LabVIEW displays both hexadecimal and normal characters in the same table

The spring recruitment in 2022 begins, and a collection of interview questions will help you

8位全加器原理

Halcon knowledge: binocular_ Discrimination knowledge

C语言刷题随记 —— 自由落体的球
随机推荐
Tiktok x-bogus and_ Signature parameter analysis
Pta:6-30 time addition
composer按装laravel
396. 矿场搭建
不归零编码NRZ
win10下安装、运行MongoDB
free( )的一个理解(《C Primer Plus》的一个错误)
PTA: price of 7-65 beverage
If you want to understand PostgreSQL, you must first brush the architecture
论文阅读_关系抽取_CASREL
Leetcode 1208. Make strings as equal as possible
Bootstrap drive, top switching power supply and Optocoupler
thinkphp6 无关联ID的连表查询(2张表)主要是select的应用
Openjudge noi 1.13 50: several
E45: ‘readonly‘ option is set (add ! to override)
项目总结1(头文件,switch,&&,位变量)
gson TypeAdapter 适配器
Gson typeadapter adapter
静态双位置继电器 XJLS-84/440/DC220V
thinkPHP6解决跳转问题