在他的2005年11月1日 C++专栏中,Herb Sutter写道...
int A[17];
int* endA = A + 17;
for( int* ptr = A; ptr < endA; ptr += 5 )
{
// ...
}
在一些CPU架构上,包括当前的一些架构,上述代码可能会导致硬件陷阱发生在创建三个末尾指针的位置,无论该指针是否被解引用。
CPU如何在位模式上触发陷阱?还有其他情况吗?
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架构”仅指已发货的产品,如果虚构作品中描述或暗示了最近出版日期的架构,则也应包括在内。