悬空指针(dangling pointer)
在C语言中,调用free§函数会释放p指向的内存块,但是不会改变p本身。如果忘记了p不再指向有效内存块,混乱可能随即出现。
If a pointer still references the original memory after it has been freed, it is called a dangling pointer.
char *p = malloc(4);
...
free(p);
...
strcpy(p, "abc"); // wrong
为了避免出现"悬空指针"引发不可预知的错误,在释放内存之后,常常会将指针p赋值为NULL。
char *p = malloc(4);
...
free(p);
p = NULL;
野指针(wild pointer)
A pointer in c which has not been initialized is known as wild pointer.
野指针(wild pointer)就是没有被初始化过的指针。
比如:
void *p;
此时p就是一个野指针。
在C中,一般定义指针时就初始化指针,来防止野指针。
void *p = NULL;
void *data = malloc(size);
参考文献
https://www.cnblogs.com/idorax/p/6475941.html
https://zhuanlan.zhihu.com/p/146477984