> 1. Breaking reflexivity is not OK. Ever. a==a should hold.
No. Once you've invoked undefined behavior, all bets are off. There's a trivial solution: Don't do that.
Once you've called realloc() and it's returned non-NULL, p is gone--for all anyone knows it points to random nothingness. Yes, it seems weird that the library was able to enlarge it in-place but the pointers end up non-equivalent. But you can't ever blindly rely on the in-place realloc() optimization anyway.
Simply put, using p blindly after a successful realloc() is a use-after-free bug, no matter what. If you use realloc() correctly you will never see this.
Note that if you've proven the equivalence to the compiler it works fine:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *p = (int*)malloc(sizeof(int));
int *q = (int*)realloc(p, sizeof(int));
if (p == q) {
*p = 1;
*q = 2;
printf("%d %d\n", *p, *q);
}
}
Production code should never do this, though (it's pointless). The safest, most correct thing to do is to stop using p after a successful realloc().