3 ms·
Modern C++ programs should never write a new or delete anymore, except in very particular circumstance. You should always be using make_unique and make_shared
by zanny 11y ago
Modern C++ programs should never write a new or delete anymore, except in very particular circumstance.
You should always be using make_unique and make_shared for heap allocations and you use shared whenever an object is being passed around and needs to track its owners, unique is for single-usage-instance use cases.
Those smart pointers have minimal overhead, deterministic runtime, and have no more overhead than manual implementations of the same mechanisms with raw pointers.
That is also how Rust does it - references are smart pointers, so you avoid having a GC.
- Retric 11y agoBeing able to handle a large self-referential data structure automatically is never cheap. So, you need to either have complex GC, a manual approach, or leak memory.
- oldmanjay 11y agoRust's lifetime tracking system is not implemented with smart pointers, although it does facilitate some very smart reference constructs indeed.
- steveklabnik 11y agoTo elaborate a bit, references are "smart pointers" in the sense that they have extra semantics on top of regular pointers, but they also aren't like what most people refer to as smart pointers. Rust's &T (references) are entirely checked at compile time, and generate the exact same code as a regular old C pointer would. No wrappers, extra allocations, reference counts, move/copy/whatever constructors (not that Rust even has those, but are important for C++ smart pointers).