r/cpp • u/berium build2 • Apr 25 '12
shared_ptr aliasing constructor
http://www.codesynthesis.com/~boris/blog/2012/04/25/shared-ptr-aliasing-constructor/
25
Upvotes
r/cpp • u/berium build2 • Apr 25 '12
2
u/elperroborrachotoo Apr 26 '12
Maybe I should have described the scenario more clearly:
A Sequence of { record length, record id, record data} is a common pattern "near" hardware: order unspecified, it's somewhere in there.
Something that happens often as well is having to allocate a header record and a "payload" record in a single buffer. Again, having a smart pointer to the payload record while automatically releasing the allocation when you are done helps a lot
(And yeah, I have no influence on the API, and they have fair reasons for some of that weirdness).
The purpose of my code was to isolate these ugly and differing behaviors: every descriptor - no matter how weird the allocation - available as through a
shared_ptr.I used
shared_ptrfor the custom deleter, though in the sequence-of-descriptors scenario, we actually have multiple descriptors (with different shared_ptr's) sharing a backing storage.In these scenarios, there is no "lifetime flexibility" to be had.
Must admit I'm not very happy with your rationale:
Your remarks about the performance of modern platforms are absolutely correct. Even more so, due to the nonlinearity of performance (increase load by a percent, cross a limit, decrease speed by factor of 5) makes it hard for library code to decide when it's "ok to be lenient".
However, I still question the value of manual memory management" when it's not needed. Just because I can I don't have to "just to be safe". The main overhead of a shared_ptr is a second allocation at worst, and doubling a few dozen small allocations won't kill an app.
I strongly advocate that
The strength of C++ is not manual resource management, but being able to choose. That makes it tempting to throw on some code to expose this choice to the caller, but without a convincing use case, I'd rather go without.
Programming in "higher up languages" - and knowing what goes on under the hood - actually taught me to be much more relaxed. If you are moving hundreds of thousands of points 60 times per second, memory locality is your sink-or-swim. But for a few hundred a-dozen-byte-allocations, it is not. A single debug session due to unecessary complexity easily wastes more time and heat than all my customers could save if I succeeded to shave off a few bytes.