Glossary · Automation software engineering and architecture
Dangling pointer
Also known as: Wild pointer, Use-after-free
German: Ungültiger Zeiger
In programming, a dangling pointer is a pointer or reference that still points to a memory location after the object stored there has been freed or has gone out of scope. Accessing it causes undefined behavior, such as data corruption, crashes or exploitable security vulnerabilities.
- Software engineering
In one sentence
A dangling pointer still refers to memory after the object there was freed or went out of scope; using it causes undefined behavior.
Example
A driver keeps a pointer to a receive buffer that the communication stack frees after a reconnect; later writes through the pointer corrupt unrelated data.
How it applies
- Engineering: Dangling pointers are typical for C and C++ code in firmware, drivers and runtimes. They are often caused by unclear ownership of memory, by returning addresses of local variables or by keeping references after Dynamic memory allocation is released.
- Prevention: Coding standards that restrict dynamic memory, clear ownership rules, smart pointers, static and dynamic analysis tools and memory-safe languages reduce the risk. In IEC 61131-3, pointers and references exist in some implementations and need the same care.
- Documentation: API documentation must state who owns memory passed across interfaces and how long returned references stay valid. Missing ownership rules in documentation are a common root cause of such defects.
Dangling pointer vs. null pointer
A null pointer points to nothing and can be checked before use. A dangling pointer looks valid but points to memory that no longer belongs to the object, so it cannot be detected by a simple check. Garbage collection prevents dangling pointers by freeing memory only when no references remain.