A pointer pointing to a memory location that has been deleted (or freed) is called dangling pointer.
// Deallocating a memory pointed by ptr causes
// dangling pointer
#include <stdlib.h>
#include <stdio.h>
int main()
{
int *ptr = (int *)malloc(sizeof(int));
// After below free call, ptr becomes a
// dangling pointer
free(ptr);
// No more a dangling pointer
ptr = NULL;
}
本文深入探讨了悬空指针的概念,即指向已删除或释放内存位置的指针。通过C语言示例代码,展示了如何在内存被释放后,将指针设置为NULL来避免悬空指针的问题。
977





