Pointer arithmetics with two different buffers

To add the standard quote:

expr.add#5

When two pointer expressions P and Q are subtracted, the type of the result is an implementation-defined signed integral type; this type shall be the same type that is defined as std::ptrdiff_­t in the <cstddef> header ([support.types]).

  • (5.1)
    If P and Q both evaluate to null pointer values, the result is 0.

  • (5.2)
    Otherwise, if P and Q point to, respectively, elements x[i] and x[j] of the same array object x, the expression P - Q has the value i−j.

  • (5.3)
    Otherwise, the behavior is undefined.
    [ Note: If the value i−j is not in the range of representable values of type std::ptrdiff_­t, the behavior is undefined.
    — end note
     ]

(5.1) does not apply as the pointers are not nullptrs. (5.2) does not apply because the pointers are not into the same array. So, we are left with (5.3) – UB.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)