|
Huh, okay, I didn't know that, but apparently it is true. Using clang, program #1: #include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
void * p = malloc(123);
void * q = realloc(p, 200);
printf("%p == %p -> %i\n", p, q, p == q);
free(q);
printf("%p\n", q);
return 0;
}
prints out: 0x5f867a8a9300 == 0x5f867a8a9300 -> 1
0x5f867a8a9300
Program #2: (only difference is the extra printf after malloc) #include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
void * p = malloc(123);
printf("%p\n", p);
void * q = realloc(p, 200);
printf("%p == %p -> %i\n", p, q, p == q);
free(q);
printf("%p\n", q);
return 0;
}
prints out: 0x5bcba8225300
0x5bcba8225300 == 0x5bcba82257a0 -> 0
0x5bcba82257a0
So if we print out the pointer before reallocation then they're not equal, but if we don't then they are equal.Funny enough, "-fsanitize=undefined" doesn't seem to detect this. Neither does "-fsanitize=address" (but with ASAN the results are now consistent and in both cases compare to not equal). |