Consider the following code:
char* p = new char[2];
long* pi = (long*) p;
assert(p == pi); // OK
char* p1 = &p[1];
long* pi1 = (long*) p1;
assert(p1 == pi1); // OK
int d = p1 - p;
int d1 = pi1 - pi;
assert(d == d1); // No :(
After this runs, I get d == 1
and d1 == 0
, although p1 == pi1
and p == pi
(I checked this in the debugger). Is this undefined behavior?