有效指针的含义 [英] Meaning of valid pointer

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

问题描述

在对答案的评论中,我偶然发现了有关有效指针"含义的讨论.由于我认为总体而言这很有趣:

In the comments to this answer I stumbled about a discussion about the meaning of "valid pointer". Since I think that that is interesting in general:

什么是C ++中的有效指针"?

What is a "valid pointer" in C++?

尤其是:

reinterpret_cast<const void*>(0x1)是有效的指针吗?

推荐答案

该标准将实现分为两大类:

The Standard places implementations into two general categories:

  • 具有严格指针安全性的人
  • 那些指针安全宽松的人

您的表达式绝对不是安全派生的指针,因此在第一个表达式中是无效的.

Your expression definitely is not a safely-derived pointer, so it's invalid in the first.

从3.7.4.3引用:

Quote from 3.7.4.3:

实现可能具有宽松的指针安全性,在这种情况下,指针值的有效性不取决于它是否是安全得出的指针值.替代地,实现可能具有严格的指针安全性,在这种情况下,除非动态引用对象的对象具有动态存储持续时间,否则该对象的值不是安全导出的指针值,否则该指针值将是无效的指针值,除非引用的完整对象先前已宣布可达(20.7.4). [注意:使用无效的指针值(包括将其传递给释放函数)的效果是不确定的,请参见3.7.4.2.即使未安全派生的指针值可能与某些安全派生的指针值进行比较也是如此. —结束说明]由实现定义,是实现是宽松的还是严格的指针安全性.

An implementation may have relaxed pointer safety, in which case the validity of a pointer value does not depend on whether it is a safely-derived pointer value. Alternatively, an implementation may have strict pointer safety, in which case a pointer value referring to an object with dynamic storage duration that is not a safely-derived pointer value is an invalid pointer value unless the referenced complete object has previously been declared reachable (20.7.4). [ Note: the effect of using an invalid pointer value (including passing it to a deallocation function) is undefined, see 3.7.4.2. This is true even if the unsafely-derived pointer value might compare equal to some safely-derived pointer value. — end note ] It is implementation defined whether an implementation has relaxed or strict pointer safety.

对于具有宽松安全性的实现,指针值的获取方式无关紧要,只需(3.9.2):

For implementations with relaxed safety, it doesn't matter how the pointer value is gotten, just that (3.9.2):

对象指针类型的有效值表示内存中字节的地址(1.7)或空指针.

A valid value of an object pointer type represents either the address of a byte in memory (1.7) or a null pointer.

0x1是系统上的有效内存地址吗?好吧,对于某些嵌入式系统而言.对于大多数使用虚拟内存的操作系统,从零开始的页面被保留为无效.

Is 0x1 a valid memory address on your system? Well, for some embedded systems it is. For most OSes using virtual memory, the page beginning at zero is reserved as invalid.

这篇关于有效指针的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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