我们可以检查指针以确保它是有效地址吗? [英] Can we check a pointer to make sure it is a valid address?

查看:78
本文介绍了我们可以检查指针以确保它是有效地址吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的想法是打印它指向的对象. 我认为有效的指针应具有有效的对象. 如果我们尝试打印出对象,我们将验证指针是否有效. 我说的对吗?

My idea is to print the object it points to. I think a valid pointer should have a valid object. If we try to print out the object we verify if the pointer is valid. Am I right?

推荐答案

我认为有效的指针应该具有有效的对象.

I think a valid pointer should have a valid object.

是的,这是有效指针的定义.

Yes, that's the definition of a valid pointer.

如果我们尝试打印出对象,则会验证指针是否有效.

If we try to print out the object we verify if the pointer is valid.

不幸的是,你不能.您可以检查指针是否为空;但是如果未正确初始化它,或者如果它指向已被破坏的对象,则它既无效也不为空.

Unfortunately, you can't. You can check whether the pointer is null; but if it wasn't initialised properly, or if it pointed to an object that's been destroyed, it will be neither valid nor null.

如果您想要一个足够聪明的指针来知道它是否有效,则需要一个智能指针.

If you want a pointer that's smart enough to know whether it's valid, you'll need a smart pointer.

这篇关于我们可以检查指针以确保它是有效地址吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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