+ 3

What is a dangling pointer?

11th Feb 2019, 9:22 AM
Mayank Mewar
Mayank Mewar - avatar
2 Answers
+ 3
If u know basics of dynamic memory allocation..... then when we allocate memory then a pointer to start of memory is returned say by syntax like int *p =(*int)malloc(size in bytes); But when u use free function to clear the memory..... The memory gets cleared and pointer is NOT POINTING TO ANYTHING..SUCH POINTER KNOWN AS DANGLING POINTERS
11th Feb 2019, 11:40 AM
Saurabh B
Saurabh B - avatar
0
An addition to Saurabh B's answer: Those "loose hanging" pointers can be avoided by setting *p = NULL after free'ing it and checking via e.g. if (*p == NULL) return 0;
26th Feb 2019, 7:36 AM
Kami Rotschild