Ama*_*ain 3 c pointers dangling-pointer
在下面的代码段中free(x),为什么会y变为0?
根据我的理解,堆中被指向x并且仍然被指向的内存y尚未分配给其他人,那么如何将其更改为0?
此外,我认为不会free(x)将其改为0.
任何意见?
#include <stdio.h>
int main(int argc, char *argv[])
{
int *y = NULL;
int *x = NULL;
x = malloc(4);
*x = 5;
y = x;
printf("[%d]\n", *y); //prints 5
free(x);
printf("[%d]\n", *y); //why doesn't print 5?, prints 0 instead
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Did*_*set 12
这是未定义的行为,解释只是猜测.
我可以推测,也许你正在运行C库的调试版本,并且free()的调试版本确实将指向区域归零.