Any real example of reinterpret_cast changing a pointer value?

后端 未结 5 2321
灰色年华
灰色年华 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:19

    Note that when the standard states that it can or cannot possibly do something, it does not mean that there is any current implementation that has that behavior, only that they could.

    The closest that I can think of is an architecture where type alignment was required by the hardware, and an implementation that decided to correct alignment if needed. Something like:

    aligned8 var;
    aligned1 *p = reinterpret_cast(&var);
    aligned1 *q = p + 1; // assuming aligned 1 size is not multiple of 8
    aligned8 *a = reinterpret_cast(q); // [1]
    

    There could be a requirement that for a to be a valid pointer it has to address a memory position multiple of 8, while the argument q with lesser alignment requirements could point to any memory address.

提交回复
热议问题