为什么悬空指针不能存储任何值,为什么它会抛出 0?因为它指向被释放的相同内存。如果我们尝试存储一些值,为什么为 0?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *p;
p=(int*)malloc(sizeof(int)*5);//allocating memory in heap
printf("%p\n",p);
free(p); //freeing memory
printf("%p\n",p); //still pointer same loaction(dangling pointer)
scanf("%d",p); // why cant i scan if it is still pointing same location
// i know memory is delete but why 0 is thrown?
printf("%d\n",*p);// i am getting zero here?
}