[问]关于免费(ptr) [英] [Q] about free(ptr)

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

问题描述



如何测试指针是否已被释放? (即使用free())?

我天真的假设是这样的指针会等于NULL,但

不是这样。


谢谢,


kj


-

注意:在我的地址中,第一期之前的所有内容都是向后;

和最后一段时间以及它之后的所有内容都应该被丢弃。


How can one test if a pointer has been "freed" (i.e. with free())?
My naive assumption was that such a pointer would equal NULL, but
not so.

Thanks,

kj

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.

推荐答案

>如何测试指针是否已被释放 (即使用free())?


你不会,除非你想冒着程序崩溃的风险。
>How can one test if a pointer has been "freed" (i.e. with free())?

You don''t, unless you want to risk having your program crash.
我的天真假设这样的指针会等于NULL,但不是这样。
My naive assumption was that such a pointer would equal NULL, but
not so.




没有什么能阻止你将指针变量设置为

将其值传递给free()后为NULL,但是没有得到其他可能存在的副本。


Gordon L .Burditt



There is nothing preventing you from setting a pointer variable to
NULL after passing its value to free(), but that doesn''t get the
other copies that might be around.

Gordon L. Burditt


kj< so *** @ 987jk.com.invalid>在消息新闻中写道:< co ********** @ reader1.panix.com> ...
kj <so***@987jk.com.invalid> wrote in message news:<co**********@reader1.panix.com>...
如何测试指针是否已经 ;释放" (即使用free())?
我天真的假设是这样的指针会等于NULL,但
不是这样。
How can one test if a pointer has been "freed" (i.e. with free())?
My naive assumption was that such a pointer would equal NULL, but
not so.




想一想这个:当你调用free(ptr)时,ptr是按值传递的,所以

free()不能修改它。我认为除了编写自己的

free()之外,没有办法知道

指针是否指向有效或无效的东西,即喜欢


void my_free(void ** ptr){

free(* ptr);

* ptr = NULL; < br $>
}


int * p = malloc(200);

....

my_free (& p);


但我认为这是不可取的。


HTH



Think about this: when you call free(ptr), ptr is passed by value, so
free() cannot modify it. I think there''s no way of knowing whether a
pointer points to something valid or not, apart from writing one''s own
free(), ie something like

void my_free(void **ptr) {
free(*ptr);
*ptr = NULL;
}

int *p = malloc(200);
....
my_free(&p);

but I think it''s not advisable.

HTH

< br>

su****@katamail.com (子网)写道:
kj< so *** @ 987jk.com.invalid>在消息中写道
新闻:< co ********** @ reader1.panix.com> ...
kj <so***@987jk.com.invalid> wrote in message
news:<co**********@reader1.panix.com>...
怎样才能测试一下指针已被释放 (即使用free())?
我天真的假设是这样的指针会等于NULL,但不是这样。
How can one test if a pointer has been "freed" (i.e. with free())?
My naive assumption was that such a pointer would equal NULL, but
not so.



你不能。一旦你调用了free(p),p的值就是

不确定。任何引用p值的尝试都会调用

未定义的行为。 (是的,这是任何尝试引用

p的价值,而不仅仅是取消引用它的任何尝试。)

想一想:当你免费拨打电话时( ptr),ptr是按值传递的,所以
free()不能修改它。我认为没有办法知道
指针是否指向有效或无效的东西,除了写一个自己的
free(),比如
void my_free(void ** ptr){
free(* ptr);
* ptr = NULL;
}
int * p = malloc(200) ;
...
my_free(& p);

但我认为这是不可取的。


You can''t. Once you''ve called free(p), the value of p is
indeterminate. Any attempt to refer to the value of p invokes
undefined behavior. (Yes, that''s any attempt to refer to the value of
p, not just any attempt to dereference it.)
Think about this: when you call free(ptr), ptr is passed by value, so
free() cannot modify it. I think there''s no way of knowing whether a
pointer points to something valid or not, apart from writing one''s own
free(), ie something like

void my_free(void **ptr) {
free(*ptr);
*ptr = NULL;
}

int *p = malloc(200);
...
my_free(&p);

but I think it''s not advisable.




这是无效的。你正在将类型为int **的参数传递给函数

,它需要一个void **。有从int *到

void *的隐式转换,但不是从int **到void **。


OP的注释:[Q]你的主题前缀没有帮助。我们

可以说这是个问题。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



It''s invalid. You''re passing an argument of type int** to a function
that expects a void**. There''s an implicit conversion from int* to
void*, but not from int** to void**.

A note to the OP: the "[Q]" prefix on your subject is not helpful. We
can tell that it''s a question.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


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

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