Persistent Pointers
Documents a memory management pattern where freed structures remain allocated to keep pointers valid, preventing dangling-pointer crashes.
What this file does
Documents a memory management pattern where freed structures remain allocated to keep pointers valid, preventing dangling-pointer crashes.
When to use it
- Designing an embedded system with high heap churn and pointer safety requirements
- Building a long-running server or daemon that must avoid segmentation faults from stale pointers
- Working on a project where pointer invalidation after free is a recurring bug source
- Evaluating trade-offs between memory usage and crash resilience in a C codebase
Assumes this stack
Persistent Pointers
In ESPShell, certain data structures — once allocated — are never truly freed.
Instead, deallocated entries are moved to an “unused entries” list, where they remain allocated but marked as reusable.
This design ensures that pointers to these structures never become invalid, even after the object is “freed.”
Why Persistent Pointers?
✔ Higher performance
Fewer malloc() calls, less heap churn.
✔ Better stability
Pointers remain valid (memory is still allocated), dramatically reducing the risk of segmentation faults.
✔ Simpler code
No need for excessive pointer checks or mutexes around allocation/reallocation patterns.
How It Works
These structures are managed through _get() / _put() pairs
(e.g., ha_get() and ha_put()).
A structure may be handed out many times, but its underlying memory block
always stays allocated, even if the object it represents is considered “freed.”
Example
struct alias {
...
char name[0]; // always valid*
};
struct ifcond {
...
struct alias *al; // always valid*
...
} *ifc; // always valid*
* “Always valid” means:
- the memory address never goes stale (remains allocated),
- but the contents may correspond to a recycled object.
Thus, accessing:
ifc->al->name
is always safe in the sense that it will not crash, because every pointer in the chain points to persistent memory.
Important Note
Persistent pointers do not magically prevent logical bugs:
- Dereferencing a pointer to a recycled object is still a semantic bug.
- The behavior may be incorrect if the object was reused.
- However, the program will not crash, making debugging far easier.
Persistent pointers allow ESPShell to remain robust under heavy internal churn while avoiding many categories of dangling-pointer crashes.
What's inside
1 section explaining the concept, 3 benefits, 1 mechanism description, 1 code example, 1 important note
Where it goes
Keep it in your repository where the agent or team that needs it will read it.
Related Documents
Zig 0.16.0 Context Document for LLMs
Documents breaking changes from Zig 0.13/0.14 to 0.16.0, covering syntax, I/O, build system, and standard library updates.
绘制对比图片 KITTI
Documents the topic remapping and data flow needed to connect Fast-LIO2 frontend with SC-PGO backend for KITTI evaluation.
Beads Workflow Context
Guides when to use bd graph-based issue tracker versus TodoWrite for session vs multi-session work, with commands and workflows.
EPIC: usda-data-crush-stats
Maps a multi-year USDA grape crush data pipeline from raw downloads through bronze, silver, and gold layers to final CSV fact and dimension tables.