这段代码有什么问题? [英] what's wrong with this code?

查看:65
本文介绍了这段代码有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我是C的新手,我正在阅读K& R,我在本书的代码中找到了

。 K& R表示,释放后释放的东西是不允许的。
并以此为例。


for(p = head; p!= NULL; p-> next)

free(p);


它有什么问题?

解决方案

好看的事情p-> next在第一次运行循环时,部分并没有真正推进指针p

,指针p被释放但是在

秒(因为它是没有提前并且已经被释放了它被释放了

秒。

现在我不记得那时会发生什么(通常它只是工作或

只是普通的崩溃,因为我很少遇到这种情况)。


希望这有帮助


TDR

cc0064263 < CA ************* @ yahoo.com>在消息中写道

新闻:DM ******************** @ comcast.com ...

你好,

我是C的新手,我正在阅读K& R,我来到这本书的代码中。 K& R说,释放后释放的东西是不正确的
并以此为例。

for(p = head; p!= NULL; p-> next)
free(p);

它出了什么问题?



在文章< DM ******************** @ comcast.com>中,cc0064263写道:

你好,

我是C的新手,我正在阅读K& R,我来到这本书的代码中。 K& R说,释放后释放的东西是不正确的
并以此为例。

for(p = head; p!= NULL; p-> next)
free(p);

它有什么问题?



至少有两个问题这段代码:p->接下来什么都不做

和p在被释放后被访问。


即使是第一个问题都没有(即代码说'p = p-> next'')

第二个仍然(可能)杀死你的pprogram。


while循环中的for循环如下所示:


p = head;

while(p!= NULL)

{

free(p);

p = p-> next;

}


或许现在为什么在免费后访问p会更清楚?


为了做到这一点,你可以这样做:


p_type p = head;

p_type last = NULL;

while(p!= NULL)

{

last = p;

p = p-> next;

free(last);

}


(其中p_type是某种指针类型)。在这里,你将p的旧值保存到

last,得到你想要的p值,然后*然后*释放保存的旧值。


HTH


rlc


-

监狱:只是另一种解释语言

Just:监狱使用愚蠢的条款


加入讨论这种语言的定义
ja *********** @ lists.sourceforge.net http://jail-ust.sourceforge.net

(发送邮件至 JA *********** @ lists.sourceforge .net


(我在第一个回复时写了一个错字)

TDR < TD ******* @ hotmail.com>在消息中写道

新闻:3f ******** @ news.tm.net.my ...

好​​看的东西p- >接着"部分并没有真正推进指针

p

作为第一次循环运行的结果,指针p被释放但是在
$ b循环的$ b第二次运行(因为它没有提前并且已经被释放)它

第二次被释放



现在我真的不记得会发生什么事情(通常它只是工作



因为我很少遇到这种情况而只是简单的崩溃)。


希望这有助于


TDR

" cc0064263" < CA ************* @ yahoo.com>在消息中写道
新闻:DM ******************** @ comcast.com ...

你好,
我是C的新手,我正在阅读K& R,我来到这本书的代码中。 K& R说,释放后释放的东西是不正确的
并以此为例。

for(p = head; p!= NULL; p-> next)
free(p);

它有什么问题?




hello,

i am very new to C and i am reading K&R and i came
accross this code in the book. K&R says that it is
incorrect to free something after it has been freed
and gives this as an example.

for (p=head; p!= NULL; p->next)
free(p);

what''s wrong with it?

解决方案

Well looks of things the "p->next" part doesn''t really advance the pointer p
as a result on the first run of the loop, the pointer p is freed but on the
second (since it was not advance and was already freed) it gets freed
second.
Now I don''t really remember what would happen then (usually it just works or
just plain crashes as I rarely met such cases).

hope this helps

TDR
"cc0064263" <ca*************@yahoo.com> wrote in message
news:DM********************@comcast.com...

hello,

i am very new to C and i am reading K&R and i came
accross this code in the book. K&R says that it is
incorrect to free something after it has been freed
and gives this as an example.

for (p=head; p!= NULL; p->next)
free(p);

what''s wrong with it?



In article <DM********************@comcast.com>, cc0064263 wrote:

hello,

i am very new to C and i am reading K&R and i came
accross this code in the book. K&R says that it is
incorrect to free something after it has been freed
and gives this as an example.

for (p=head; p!= NULL; p->next)
free(p);

what''s wrong with it?


There are at least two problems with this code: p->next doesn''t do anything
and p is accessed after it''s been freed.

Even if the first problem weren''t there (i.e. the code said `p = p->next'')
the second would still (possibly) kill your pprogram.

The for loop in while form looks like this:

p = head;
while (p != NULL)
{
free(p);
p = p->next;
}

perhaps it is a bit clearer now why p is accessed after the free?

To do it right, you can do it like this:

p_type p = head;
p_type last = NULL;
while (p != NULL)
{
last = p;
p = p->next;
free(last);
}

(in which p_type is some pointer type). Here, you save the old value of p to
last, get the value you want for p, and *then* free the saved, old value.

HTH

rlc

--
Jail: Just Another Interpreted Language
Just: Jail Uses Silly Terms

Join the discussion on the definition of this language at
ja***********@lists.sourceforge.net http://jail-ust.sourceforge.net
(send mail to ja*********************@lists.sourceforge.net)


( I made a typo on the first reply)
"TDR" <td*******@hotmail.com> wrote in message
news:3f********@news.tm.net.my...
Well looks of things the "p->next" part doesn''t really advance the pointer
p
as a result on the first run of the loop, the pointer p is freed but on the
second run of the loop (since it was not advance and was already freed) it
gets freed a
second time.
Now I don''t really remember what would happen then (usually it just works
or
just plain crashes as I rarely met such cases).

hope this helps

TDR

"cc0064263" <ca*************@yahoo.com> wrote in message
news:DM********************@comcast.com...

hello,

i am very new to C and i am reading K&R and i came
accross this code in the book. K&R says that it is
incorrect to free something after it has been freed
and gives this as an example.

for (p=head; p!= NULL; p->next)
free(p);

what''s wrong with it?




这篇关于这段代码有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆