Any real example of reinterpret_cast changing a pointer value?

后端 未结 5 2330
灰色年华
灰色年华 2021-02-08 07:17

According to C++ Standard, a reinterpret_cast of a pointer T* to some other type pointer Q* can change or not change the pointer value dep

5条回答
  •  南笙
    南笙 (楼主)
    2021-02-08 08:05

    The most likely source of trouble is on a vector machine where scalar operations are defined in terms of vectors, and a scalar pointer consists of a pointer to vector with an index into the vector. Historically the original Cray architecture was like this and it caused headaches. Nowadays you might see something like that on a GPU, but I can't point something specific off the top of my head.

    The most likely effect is truncation as the destination pointer type lacks bits to specify the index part. C++11 gives a nod in this direction by allowing any pointer types to be reinterpret_casted as long as they have same alignment requirements. The bits "zeroed" by stringent alignment are allowed to not exist.

    An object pointer can be explicitly converted to an object pointer of a different type. When a prvalue v of type “pointer to T1” is converted to the type “pointer to cv T2”, the result is static_cast(static_cast(v)) if both T1 and T2 are standard-layout types (3.9) and the alignment requirements of T2 are no stricter than those of T1, or if either type is void. Converting a prvalue of type “pointer to T1” to the type “pointer to T2” (where T1 and T2 are object types and where the alignment requirements of T2 are no stricter than those of T1) and back to its original type yields the original pointer value. The result of any other such pointer conversion is unspecified.

提交回复
热议问题