C++
C++ smart pointers: unique_ptr, shared_ptr and RAII
Every place you write new is a place you might forget delete. Modern C++ solves this with RAII and smart pointers. Here's when to use unique_ptr, shared_ptr and weak_ptr, with examples.
People starting C++ are taught new and delete, and then told for years: "don't forget delete". But forgetting isn't the only problem. When an exception is thrown halfway through a function, when an early return is added, or when the same object is deleted in two places, even the most careful programmer can slip. Modern C++ solves this with one idea: RAII.
RAII: a resource tied to an object's lifetime
RAII (Resource Acquisition Is Initialization) means a resource (memory, a file, a lock, a socket) is acquired in an object's constructor and released in its destructor. When the object goes out of scope, whether normally or through an exception, the destructor always runs. So the resource is always released.
{
std::ifstream file("data.txt"); // file opened
// ... read ...
} // scope ends: the file closes itselfSmart pointers are the same idea applied to memory.
unique_ptr: a single owner
std::unique_ptr is the sole owner of an object. It can't be copied, only moved. When it goes out of scope, it deletes the object.
#include <memory>
auto player = std::make_unique<Player>("Fahrettin");
player->move();
auto other = std::move(player); // ownership transferred; player is now emptyThis should be your default choice. Its overhead is essentially zero; it's as fast as a raw pointer, but it can't leak.
shared_ptr: shared ownership
If an object genuinely needs several owners, use std::shared_ptr. It contains a reference count; when the last owner goes away, the object is deleted.
auto texture = std::make_shared<Texture>("grass.png");
scene.add(texture);
inventory.add(texture); // both share the same textureIt has a cost: the count is updated atomically and an extra control block is kept. That's why a "shared_ptr everywhere" habit both hurts performance and blurs who actually owns what.
weak_ptr and cycles
If two objects hold each other with shared_ptr, their counts never reach zero and neither is ever deleted. That's the classic way to leak memory even with smart pointers. The fix is to make one side of the relationship a std::weak_ptr: it points to the object but doesn't increase the count.
struct Node {
std::shared_ptr<Node> next;
std::weak_ptr<Node> prev; // breaks the cycle
};Practical rules
- Don't write
newanddeletedirectly; usestd::make_uniqueandstd::make_shared. - Default to
unique_ptr; useshared_ptronly when shared ownership is really needed. - If a function only uses an object, pass a reference or raw pointer, not the smart pointer. Ownership and usage are different things.
- In mutual relationships, make one side a
weak_ptr.
Smart pointers taught me something about C++: when a language makes the right habits easy and the wrong ones hard, people make fewer mistakes. Knowing how to hunt leaks with Valgrind and AddressSanitizer is still valuable, but the best leak is the one that never happens.