理由比较指针数组外面是UB [英] Rationale for pointer comparisons outside an array to be UB

查看:129
本文介绍了理由比较指针数组外面是UB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,标准(指 N1570 )称,比较指针如下:

So, the standard (referring to N1570) says the following about comparing pointers:

C99 6.5.8 / 10关系运算符

C99 6.5.8/5 Relational operators

当两个指针进行比较,结果取决于相对上
  中的对象的地址空间中的位置指向。
  ... [喀嚓比较明显的定义,聚集内] ...
  在其他情况下,
  该行为是不确定的。

When two pointers are compared, the result depends on the relative locations in the address space of the objects pointed to. ... [snip obvious definitions of comparison within aggregates] ... In all other cases, the behavior is undefined.

什么是UB的这个实例的理由,而不是指定(例如)转化为使用intptr_t 和这种对比?

What is the rationale for this instance of UB, as opposed to specifying (for instance) conversion to intptr_t and comparison of that?

有一些机器架构,其中三分球上一个明智的总排序是很难构建?有一些类优化或分析说,无限制的指针比较会阻碍?

Is there some machine architecture where a sensible total ordering on pointers is hard to construct? Is there some class of optimization or analysis that unrestricted pointer comparisons would impede?

删除的答案<一个href=\"http://stackoverflow.com/questions/16544645/strange-wording-in-the-standard-concerning-comparrison-of-pointers\">this问题提到,这片UB的允许跳过的段寄存器比较和对比仅偏移。是,特别是preserve有价值?

A deleted answer to this question mentions that this piece of UB allows for skipping comparison of segment registers and only comparing offsets. Is that particularly valuable to preserve?

(这同样被删除的答案,还有一个在这里,请注意,在C ++中,的std ::少之类的都需要在三分球共实现订单,无论是正常的比较操作不还是不行。)

(That same deleted answer, as well as one here, note that in C++, std::less and the like are required to implement a total order on pointers, whether the normal comparison operator does or not.)

推荐答案

在UB邮件列表讨论各种意见的理由为&LT;不是指针上共订单?强烈暗示分段架构作为理由。包括跟随评论, 1

Various comments in the ub mailing list discussion Justification for < not being a total order on pointers? strongly allude to segmented architectures being the reason. Including the follow comments, 1:

另外,我认为,核心语言应该简单地认识到一个事实,即所有的机器,这些天有一个平面内存模式。

Separately, I believe that the Core Language should simply recognize the fact that all machines these days have a flat memory model.

2

然后,我们可能需要一个新类型,保证了全序时
  从一个指针转换(例如,在分割的结构中,转换
  将需要采取段寄存器的地址,并添加
  偏移存储在指针)。

Then we maybe need an new type that guarantees a total order when converted from a pointer (e.g. in segmented architectures, conversion would require taking the address of the segment register and adding the offset stored in the pointer).

3

指针,而不是历史的全序,实际上是这样
  今天存在的所有系统,与象牙塔外
  该委员会的头脑,所以点是没有实际意义。

Pointers, while historically not totally ordered, are practically so for all systems in existence today, with the exception of the ivory tower minds of the committee, so the point is moot.

4

不过,即使分段结构,虽然不太可能是,做出来
  回,排序问题仍具有如std加以处理,::少
  必须完全为了指针。我只是想运营商的LT;是一个
  替代拼写该属性。

But, even if segmented architectures, unlikely though it is, do come back, the ordering problem still has to be addressed, as std::less is required to totally order pointers. I just want operator< to be an alternate spelling for that property.

为什么要其他人pretend受苦(我的意思是pretend,
  由于该委员会的小队伍之外,人们已经
  假设指针是完全相对于运营商的LT命令)至
  遇到一些目前不存在的理论需求
  架构?

Why should everyone else pretend to suffer (and I do mean pretend, because outside of a small contingent of the committee, people already assume that pointers are totally ordered with respect to operator<) to meet the theoretical needs of some currently non-existent architecture?

柜台从的 UB邮件评论的趋势列表中,FUZxxl指出,支持DOS是一个理由不支持全序的指针。

Counter to the trend of comments from the ub mailing list, FUZxxl points out that supporting DOS is a reason not to support totally ordered pointers.

更新

这也是由批注C ++参考手册(支持的 ARM ),它说,这是由于支持该分段上的负担架构:

This is also supported by the Annotated C++ Reference Manual(ARM) which says this was due to burden of supporting this on segmented architectures:

这位前pression可能无法评估对分段架构假
  [...]这解释了为什么加法,减法和比较
  指针仅用于指针到一个数组和一个元素定义
  超出端。 [...]的机器用nonsegmented地址的用户
  空间开发成语,然而,提及以外的元素
  数组[...]年底是不能移植到分段架构
  除非特别的努力,被送往[...]允许[...]将是代价高昂
  全心全意一些有用的目的。

The expression may not evaluate to false on segmented architectures [...] This explains why addition, subtraction and comparison of pointers are defined only for pointers into an array and one element beyond the end. [...] Users of machines with a nonsegmented address space developed idioms, however, that referred to the elements beyond the end of the array [...] was not portable to segmented architectures unless special effort was taken [...] Allowing [...] would be costly and serve few useful purposes.

这篇关于理由比较指针数组外面是UB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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