I'm trying to explain to someone why they have a dangling pointer and how free actually works (and that pointers are values and thus are passed-by-value), but for that I think I need a way to print pointers that isn't "indeterminate" (as is the case with printf("%p", ptr)).
Would memcpy do the trick?
char buf1[sizeof(char *)];
char buf2[sizeof(char *)];
char *malloced = malloc(10);
memcpy(buf1, &malloced, sizeof(char *));
free(malloced);
memcpy(buf2, &malloced, sizeof(char *));
for (int i=0; i<sizeof(char *); i++) {
printf("%hhd %hhd / ", buf1[i], buf2[i]);
}
%p. – Ry-♦ Jul 14 '18 at 23:24deleted is UB, and OP is trying to work around that. – HolyBlackCat Jul 14 '18 at 23:26freeis defined asvoid free(void *ptr)notvoid free(void **ptr_to_ptr). it cannot actually change the pointer as far as I'm aware. – SoniEx2 Jul 14 '18 at 23:50