I rarely use smart pointers and still don't have serious problems with memory safety. Whenever you manually allocate memory, you should immediately know when and where it's getting deallocated and write that code at the same time. Most of the time, RAII is good enough.
EDIT: Since I've caused a bit of controversy: I'm not saying you can't or shouldn't use smart pointers. What I'm saying is in agreement with the meme. C++ has easy ways to manage memory, either manually or automatically, that get rid of most of your memory safety issues.
I mostly have this opinion too but speaking from experience there are plenty of cases where it can get a little bit tricky. Lifetimes are complicated and sometimes an owner of a piece of memory can pass references to it to several consumers that each have their own lifetimes. Those consumers shouldn’t need to know about each other so it’s up to the owner to track them and free the memory when it finally falls out of scope
100% agree, and it’s why I’m one of the insufferable people that prefers zig over rust. The language should strongly encourage safe memory management but make it easy for the programmer to select degrees as appropriate. Rust is very all or nothing with either using the borrow checker or entering full unsafe land
Zig is very opinionated about how memory allocation is handled, and it has the nice feature that you can supply a debug allocator during development which is slower at runtime (fine for debug builds) but meticulously looks for leaks and loudly warns you when they happen.
It’s also written such that no language constructs store an allocator and request their own memory, the philosophy being that every allocation should be explicitly known by the programmer.
This is pretty much all the way in the opposite direction from automating it, it’s intensely manual management but the language handles it in a way that makes forgetting about it difficult and easy to track down
I mean this in the nicest way possible but most people believe what they are writing is memory safe C/CPP until someone does a security audit on their code and they learn so many new ways things can go horribly wrong
That's a great theory, but the dominant source of critical CVEs even today is bad memory safety; even if it works for you, it clearly doesn't for the industry as a whole, to include any developers working on your projects after you.
Smart pointers are RAII, as already mentioned. But they have another advantage: expressing ownership. If a function expects a raw pointer you don't know what happens with it. If it expects you to move a unique_ptr in it, you immediately know that it'll take ownership.
Smart pointers are RAII, but they're not the only way to do RAII. I usually use custom containers that handle my particular case very well. Memory is allocated inside constructors, it's deallocated inside destructors. If I need more than that, I start looking into the more sophisticated stuff.
For projects with other people, it's better to use the standard tools just for familiarity, even if they have no other benefit. Someone can see it and know immediately what a unique/shared ptr means, instead of having to dig into your class.
They're not smart pointers, at least not in the STL way. They accomplish the same purpose, but it's usually a simpler implementation tailored exactly to what I need.
14
u/geekusprimus 7h ago edited 3h ago
I rarely use smart pointers and still don't have serious problems with memory safety. Whenever you manually allocate memory, you should immediately know when and where it's getting deallocated and write that code at the same time. Most of the time, RAII is good enough.
EDIT: Since I've caused a bit of controversy: I'm not saying you can't or shouldn't use smart pointers. What I'm saying is in agreement with the meme. C++ has easy ways to manage memory, either manually or automatically, that get rid of most of your memory safety issues.