即使指针永远不被取消引用,在三个结束指针中的硬件陷阱如何发生? [英] How does a hardware trap in a three-past-the-end pointer happen even if the pointer is never dereferenced?

查看:129
本文介绍了即使指针永远不被取消引用,在三个结束指针中的硬件陷阱如何发生?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Herb Sutter在他的 2005年11月1日C ++栏中撰写...

In his November 1, 2005 C++ column, Herb Sutter writes ...

int A[17];
int* endA = A + 17;
for( int* ptr = A; ptr < endA; ptr += 5 )
{
  // ...
}




[O] n某些CPU架构,包括
当前版本,上述代码
可以导致在
处发生硬件陷阱,创建三个过去的结束
指针的点,无论该
指针是否被取消引用。

[O]n some CPU architectures, including current ones, the aforementioned code can cause a hardware trap to occur at the point where the three-past-the-end pointer is created, whether that pointer is ever dereferenced or not.

CPU如何在bitpattern上陷阱?

How does a CPU trap on a bitpattern? What about ...

int A[17];

// (i) hardware will trap this ?
int *pUgly = A + 18; 

// (ii) hardware will trap this, too?
int *pEnd = A + 17;
++pEnd;  

// (iii) will this fool it?
int *precious = A + 17;
unsigned long tricksy = reinterpret_cast<unsigned long>(precious) ; 
++tricksy;
int *pHobbits = reinterpret_cast<int *>(tricksy);

奖金问题:短语某些当前CPU架构通常理解为仅指运输产品,或者如果他们被描述或暗示的小说的作品有最近的出版日期,它是否也包括虚构的建筑物?

Bonus question: Should the phrase "some current CPU architectures" be ordinarily understood to refer to shipping products only, or does it include imaginary architectures as well if the work of fiction in which they are described or alluded to has a recent publication date?

推荐答案

指针操作是与实现相关的。

Pointer operations are implementation-dependent.

可能会发生在某些平台上只允许特定的寄存器存储指针值(只有特定的寄存器可以作为索引寄存器),并且立即检查由非特权程序代码写入这种寄存器的值是否是有效地址。在这种情况下,如果指针值对应于在程序的地址空间中不存在的地址,则肯定会发生硬件陷阱。

It can happen that on some platform only specific registers are allowed for storing pointer values (only specific registers can serve as index registers) and the value written into such register by a non-priviledged program code is immediately checked for being a valid address. In this case if the pointer value corresponds to an address not present in the address space of the program the hardware trap will certainly occur.

如果是这样,由编译器分配一个新值给指针可能会导致陷阱。

If that's the case any code not optimized out by the compiler that assigns a new value to a pointer can potentially cause a trap.

这篇关于即使指针永远不被取消引用,在三个结束指针中的硬件陷阱如何发生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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