Interview Question

What is dangling pointer?

A dangling pointer refers to an object whose lifetime has ended.

💡 Concept ✅ Quick Revision ⚙ C

Answer

A dangling pointer holds a value that no longer designates a live object. • It can arise after free or after an automatic object’s lifetime ends. • Reading or writing through it has undefined behavior. • Setting one pointer to NULL does not repair other aliases to the expired object.

💡 C Example

int *pointer = malloc(sizeof *pointer); if (pointer != NULL) { free(pointer); pointer = NULL; }

⚡ Quick Revision

A dangling pointer refers to an object whose lifetime has ended.