自由() [英] free()

查看:100
本文介绍了自由()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨组,


我有一个问题要问你们所有人。我使用ptr =(int *)malloc()分配了一些

内存。现在我想用free((void *)ptr)释放那个记忆

。我的问题是free()make ptr NULL吗?如果

,我们怎么知道ptr内存是否已经被释放,而且我们不应该尝试两次释放相同的内存。手册页没有告诉

关于这个行为的任何事情。


谢谢,

Hi group,

I have a question to ask you all. I have allocated some chunk of
memory using ptr=(int*)malloc(). now I am trying to free that memory
using free((void*)ptr). My question is does free() make ptr NULL? If
not how do we know whether ptr memory has been freed already and we
should not try to free the same memory twice. Man pages does not tell
anything about this behavoiur.

Thanks,

推荐答案

dbansal写道:
dbansal wrote:

嗨组,


我有一个问题要问你们所有人。我使用ptr =(int *)malloc()分配了一些

内存。
Hi group,

I have a question to ask you all. I have allocated some chunk of
memory using ptr=(int*)malloc().



我非常怀疑它。看看过去几天关于正确使用malloc的消息在

上的分数。

I very much doubt it. Have a look a the the scores of messages over the
past couple of days regarding the correct use of malloc.


现在我正在尝试使用free((void *)ptr)释放内存


now I am trying to free that memory
using free((void*)ptr).



为什么你会在地球上施放指针?

Why on Earth would you cast the pointer?


我的问题是免费()制作ptr空值?
My question is does free() make ptr NULL?



不,怎么可能?

No, how could it?


如果不是,我们怎么知道ptr内存是否已被释放已经和我们

不应该尝试两次释放相同的内存。手册页不会告诉

这个行为的任何内容。
If not how do we know whether ptr memory has been freed already and we
should not try to free the same memory twice. Man pages does not tell
anything about this behavoiur.



这是未定义的行为。你必须管理自己的指针。


-

Ian Collins。

It''s undefined behaviour. You have to manage your own pointers.

--
Ian Collins.


dbansal说:
dbansal said:

嗨组,


我有一个问题要问你们所有人。我使用ptr =(int *)malloc()分配了一些

内存。
Hi group,

I have a question to ask you all. I have allocated some chunk of
memory using ptr=(int*)malloc().



放弃毫无意义的混淆演员。

Drop the pointless obfuscatory cast.


现在我想释放那个记忆

使用free((void *)ptr)。
now I am trying to free that memory
using free((void*)ptr).



那个。

And that one.


我的问题是free()make ptr是否为空?
My question is does free() make ptr NULL?



No.

No.


如果

我们怎么知道是不是ptr内存已经被释放
If
not how do we know whether ptr memory has been freed already



free(ptr);

ptr = NULL;


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名, - www。

free(ptr);
ptr = NULL;

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


2月10日上午7:45,dbansal ; < bansal ... @ gmail.comwrote:
On Feb 10, 7:45 am, "dbansal" <bansal...@gmail.comwrote:

嗨组,


我有一个问题要问你所有人。我使用ptr =(int *)malloc()分配了一些

内存。现在我想用free((void *)ptr)释放那个记忆

。我的问题是free()make ptr NULL吗?
Hi group,

I have a question to ask you all. I have allocated some chunk of
memory using ptr=(int*)malloc(). now I am trying to free that memory
using free((void*)ptr). My question is does free() make ptr NULL?



你不知道你在说什么。想想它一个

小秒......免费是一个按价值传递ptr的函数,那么你认为它是如何设置它的?为NULL?

You have no clue what you''re talking about. Think about it for one
tiny second... free is a function that''s passed ptr by value, so how
on earth do you imagine it could set it to NULL?


如果

我们怎么知道ptr内存是否已经被释放了我们

不应该尝试两次释放相同的内存。手册页并没有告诉

关于这个行为的任何事情。
If
not how do we know whether ptr memory has been freed already and we
should not try to free the same memory twice. Man pages does not tell
anything about this behavoiur.



也许这些手册页的作者假设他们的读者会有一些少量的情报和/或投入了一些时间

在尝试编程之前学习C语言?


-

" C的一大优势来自它最初的日子一直是它操纵角色序列的能力。

- PJ Plauger


Maybe the writers of the man pages assumed their readers would have
some small amount of intelligence and/or have invested some time in
learning the C language before trying to program in it?

--
"One of the great strengths of C from its earliest days has been its
ability to manipulate sequences of characters."
- P. J. Plauger


这篇关于自由()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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