为什么我不需要检查引用是否无效/ null? [英] Why don't I need to check if references are invalid/null?

查看:149
本文介绍了为什么我不需要检查引用是否无效/ null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读 http://www.cprogramming.com/tutorial/references.html ,它说: / p>


一般来说,引用总是
是有效的,因为你必须始终
初始化引用。这意味着
禁止一些奇怪的
情况(见下面),你可以是
确定使用引用只是
像使用一个普通的老非引用
变量。你不需要检查
确保引用不是
指向NULL,并且你不会得到
被未初始化的引用
咬了你忘了分配内存
for。


我的问题是如何知道对象的内存没有被释放/删除



这是我不能接受这个建议的信心,我需要一个更好的解释。


解决方案

你不能知道如果引用无效:



无法知道您的引用是否引用有效内存,除非注意如何使用引用。例如,如果您不确定内存将被删除,您不想使用在堆上创建的内容的引用。



您也不能知道您使用的指针是指向有效内存还是不指向有效内存。



你可以使用指针和引用进行NULL检查,但通常你不会对引用进行NULL检查,因为没有人会写这样的代码:

  int * p = 0; 
int& r = * p; //没有人这样做
if(& r!= 0)//因此没有人做这种检查
{
}








您可能希望在这种情况下使用参考:

  //我想函数fn不能复制cat并使用
//与在
中传递的对象的内存相同void fn(Cat& cat)
{
//使用cat执行操作
}

// ... main ...
Cat c;
fn(c);








使用引用来引导自己比使用指针更难。



例如:

  int * p; 
if(true)
{
int x;
p =& x;
}

* p = 3; //运行时错误

你不能做这样的事情与引用,因为引用必须用它的值初始化。



你仍然可以使用引用来拍摄自己的脚,但是你必须尝试这样做。



例如:

  int * p = new int; 
* p = 3;
int& r = * p;
delete p;
r = 3; //运行时错误


Reading http://www.cprogramming.com/tutorial/references.html, it says:

In general, references should always be valid because you must always initialize a reference. This means that barring some bizarre circumstances (see below), you can be certain that using a reference is just like using a plain old non-reference variable. You don't need to check to make sure that a reference isn't pointing to NULL, and you won't get bitten by an uninitialized reference that you forgot to allocate memory for.

My question is how do I know that the object's memory hasn't been freed/deleted AFTER you've initialized the reference.

What it comes down to is that I can't take this advice on faith and I need a better explanation.

Can anyone shed some light?

解决方案

You can't know if references are invalid:

There is no way to know if your reference is referencing valid memory except by taking care of how you use references. For example you don't want to use a reference with something created on the heap if you are unsure when the memory will be deleted.

You also can never know whether the pointer you are using is pointing to valid memory or not as well.

You can do NULL checks with both pointers and references but typically you would never do a NULL check with a reference because no one would ever write code like this:

int *p = 0;
int &r = *p;//no one does this
if(&r != 0)//and so no one does this kind of check
{
}


When to use a reference?

You probably want to use references in cases like this:

//I want the function fn to not make a copy of cat and to use
// the same memory of the object that was passed in
void fn(Cat &cat)
{
   //Do something with cat
}

//...main...
Cat c;
fn(c);


Shooting yourself in the foot is hard with references:

It's much harder to shoot yourself in the foot with references than it is with pointers.

For example:

int *p;
if(true)
{
  int x;
  p = &x;
}

*p = 3;//runtime error

You can't do this sort of thing with references since a reference must be initialized with it's value. And you can only initialize it with values that are in your scope.

You can still shoot yourself in the foot with references, but you have to REALLY try to do it.

For example:

int *p = new int;
*p = 3;
int &r = *p;
delete p;
r = 3;//runtime error

这篇关于为什么我不需要检查引用是否无效/ null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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