r/cprogramming • u/lehmagavan • Aug 27 '26
Easing memory management with a shared pointer
I recently developed a C (11 and newer) implementation of a thread-safe shared pointer with atomic reference counting:
https://github.com/andrzejs-gh/SHPTR
It supports both strong and weak references and a swappable destructor. Initialization performs a single allocation.
If anyones interested, take a look. Feedback and bug reports very much welcome.
0
Upvotes
3
u/pjl1967 Aug 27 '26
The extra level of indirection of having a pointer to a shared pointer ins't that great. Just define the structure in the header so you can have stack-based shared pointers as well as structure members that are shared pointers. It also allows simple functions to be made inline for performance.
You also can't rely on atomics existing since they're an optional feature. If not available, you either have to fall back to using mutexes or simply saying the platform isn't supported.
There's also no reason to return
dtor_ptrby pointer — just return it by value.Nit: this line:
can be just:
2
u/thradams Aug 27 '26 edited Aug 27 '26
you can do something like :
Code:
1
u/SLOOT_APOCALYPSE Aug 29 '26
sounds like an old command they added to N64 at the end of its life cycle that was never used in an game - mark but as dirty - because multiple pointers, but only one is currently loaded so it marks the others as not on the index (4mb of ram issues) could've helped with redundant cycles/searches to find the correct pointer adress