A dangling pointer is a type of memory management error originating from the mismanagement of a pointer. It occurs when a pointer is pointing to an address in memory it should no longer be pointing to, for example if the chunk of memory has been freed() but the pointer is still dereferenced.

Example

int* foo(){
	int x = 42;
	return &x;
}
 
int main(){
	int* a = foo();
	*a = 43; //dangling pointer dereference
}

Can you tell what’s wrong here? Foo does indeed return the address to the integer x, but because the integer x is a local variable to foo, it will be removed from the stack when foo completes, meaning that the address in memory that a is pointing to is no longer valid once foo completes. This is undefined behavior.