如何检查指针是否未绑定? [英] how to check if a pointer is unbound?

查看:64
本文介绍了如何检查指针是否未绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我尝试了以下程序:


包括< iostream>


使用命名空间std;


A级{

public:

int x;

A():x(10){}

};


int main(){

A a;

A * p =& a;

A * q = p;

删除p;

cout< < q<<结束;

返回0;

}


因为q应该是未绑定的,我认为输出将是0。

然而,结果是分段错误。如何检查

指针是否未绑定?


谢谢,

Jess

Hello,

I tried a program as follows:

include<iostream>

using namespace std;

class A{
public:
int x;
A():x(10){}
};

int main(){
A a;
A* p = &a;
A* q = p;
delete p;
cout << q << endl;
return 0;
}

Since "q" should be unbound, I thought the output would be "0".
However, the result was segmentation fault. How can I check if a
pointer is unbound?

Thanks,
Jess

推荐答案

Jess写道:
Jess wrote:

因为" q"应该是未绑定的,我认为输出将是0。

然而,结果是分段错误。如何检查

指针是否未绑定?
Since "q" should be unbound, I thought the output would be "0".
However, the result was segmentation fault. How can I check if a
pointer is unbound?



你不能以标准的方式。但你可以使用更安全的接近

智能指针。


问候,


Zeppe

You can''t, in a standard way. But you can use safer approached as
smart-pointers.

Regards,

Zeppe


Jess写道:
Jess wrote:

>

因为" q"应该是未绑定的,我认为输出将是0。

然而,结果是分段错误。如何检查

指针是否未绑定?
>
Since "q" should be unbound, I thought the output would be "0".
However, the result was segmentation fault. How can I check if a
pointer is unbound?



否。程序员需要管理指针。以前传递给delete的指针值的任何访问权限都是未定义的。任何

都可能发生从分段错误到显然正常工作。


删除rvalue,它不能改变它的操作。


没有办法测试指针是否被绑定。


如果你想保持指针并需要标记

它已被删除,常见的方法是将变量设置为空指针(0)的

No. It is up to the programmer to manage pointers. Any access
to pointer value previously passed to delete is undefined. Anything
can happen from segmentation fault to apparently working normally.

Delete takes an rvalue, it can not change it''s operanrd.

There''s no way to test whether a pointer is bound.

If you wish to keep the pointer around and need to flag that
it has been deleted, the common way is to set the variable to
the null pointer (0).




" Ron Natalie" < ro*@spamcop.net写信息

news:46 ********************** @ news.newshosting.com。 ..

"Ron Natalie" <ro*@spamcop.netwrote in message
news:46**********************@news.newshosting.com ...

Jess写道:
Jess wrote:

>>
自q以来应该是未绑定的,我认为输出将是0。
然而,结果是分段错误。如何检查
指针是否未绑定?
>>
Since "q" should be unbound, I thought the output would be "0".
However, the result was segmentation fault. How can I check if a
pointer is unbound?



否。程序员需要管理指针。以前传递给delete的指针值的任何访问权限都是未定义的。任何

都可能发生从分段错误到显然正常工作。


删除rvalue,它不能改变它的操作。


没有办法测试指针是否被绑定。


如果你想保持指针并需要标记

它已被删除,常见的方法是将变量设置为空指针(0)

No. It is up to the programmer to manage pointers. Any access
to pointer value previously passed to delete is undefined. Anything
can happen from segmentation fault to apparently working normally.

Delete takes an rvalue, it can not change it''s operanrd.

There''s no way to test whether a pointer is bound.

If you wish to keep the pointer around and need to flag that
it has been deleted, the common way is to set the variable to
the null pointer (0).



这不起作用。你删除了OP帖子的相关部分:

int main(){

A a;

A * p =& ; a;

A * q = p;

删除p;

cout<< q<< endl;

返回0;

}

所以,如果他添加了会发生什么

p = NULL;

删除后p?


他仍有同样的问题,因为他正在尝试使用/ q /,而不是/ p /

你说他应该设置q = NULL

也是不合理的。这些可能发生在不同的模块中,当他删除p,

时,该模块无法知道其他指针是否已将

设置为指向同一位置作为p。

-

Fred L. Kleinschmidt

波音助理技术研究员

航空稳定性与控制计算

That will not work. You removed the relevant part of the OP''s post:
int main(){
A a;
A* p = &a;
A* q = p;
delete p;
cout << q << endl;
return 0;
}
So, what happens if he adds
p=NULL;
after deleting p?

He still has the same problem, since he is trying to use /q/, not /p/
And it it is not reasonable for you to say that he should set q=NULL
too. These may occur in different modules, and when he deletes p,
there is no way for that module to know whether other pointers have
been set to point to the same place as p.
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Aero Stability and Controls Computing


这篇关于如何检查指针是否未绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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