DEV Community

Discussion on: When does a pointer become dangling in C/C++? | Why isn't this example dangling?

Collapse
 
pgradot profile image
Pierre Gradot

Just to confirm the other comment: the pointer is dangling.

It points to a variable that no longer exists.

The address of this variable (which is the value of the pointer) hasn't been reused yet so when you read the memory at this adress, you find the value of your dead variable.

This is (almost) pure coincidence.

This is why dangling pointers (or references) are so complicated to debug: you may observe an invalid behavior only once in a while.