【发布时间】:2015-05-21 09:42:13
【问题描述】:
假设我有一个有效的指针p0:
T a[10];
T* p0 = &a[0];
我知道我可以像这样安全地往返投射:
reinterpret_cast<T*>(reinterpret_cast<uintptr_t>(p0)) == p0;
但是执行以下操作是否安全?
T* p1 = reinterpret_cast<T*>(reinterpret_cast<uintptr_t>(p0) + sizeof(T));
即我可以确定没有UB和p1 == &a[1]吗?
【问题讨论】:
-
“我可以确定没有无符号字节”是什么意思? 在哪里?
-
@theV0ID UB 是 C++ 世界中未定义行为的流行首字母缩写词。
-
我很确定这在字寻址系统上会失败,其中将地址增加
n使其指向n*wordsize字节。 -
@theV0ID:未定义的行为,不是无符号字节。
标签: c++ undefined-behavior reinterpret-cast pointer-conversion