What are Smart Pointers?
In Modern C++ (C++11+), you should almost never use the
new and delete keywords manually. They are dangerous and lead to memory leaks (forgetting to delete) or dangling pointers (deleting twice). Instead, we use Smart Pointers, which automatically handle memory cleanup using the principle of RAII.cpp
Comparisons: Which one to use?
| Pointer Type | Ownership | Behavior |
|---|---|---|
| std::unique_ptr | Sole Owner | Cannot be copied; only moved. (90% use case!) |
| std::shared_ptr | Multiple Owners | Uses "Reference Counting." Deleted when count is 0. |
| std::weak_ptr | Observation | Links to a shared_ptr without increasing its count. |
1. std::unique_ptr (The 90% Solution!)
Use this for any object where there's only one clear owner. It's the most high-performance smart pointer since it adds zero speed or memory overhead compared to a raw pointer.
cpp
2. std::shared_ptr (Multiple Owners)
Use this when you have an object that is shared between many different parts of your program, and you're not sure which one will finish last.
cpp
3. std::weak_ptr (Breaking Cycles)
Wait! If two
shared_ptr objects point to each other, their reference counts will never hit zero, and they'll never be deleted! This is a memory leak. Use weak_ptr to break these cycles.cpp
Why use make_ functions?
Always use
std::make_unique and std::make_shared instead of passing new to the constructor.- Safety: Prevents memory leaks if an exception occurs during construction.
- Performance:
make_sharedis faster because it does one memory allocation instead of two.
Summary
| Term | Description |
|---|---|
| RAII | Resource Acquisition Is Initialization |
| unique_ptr | One owner; no overhead (Use by default!) |
| shared_ptr | Shared owner; small reference-count overhead |
| weak_ptr | Break reference cycles; safely observe objects |
| make_unique | Standard way to create a unique_ptr (C++14+) |
| make_shared | Standard way to create a shared_ptr (C++11+) |
| Best For | Memory safety, modern C++, leak prevention |
| Move | Transfer ownership using std::move() |
| nullptr | All smart pointers are nullptr if uninitialized |
| reset() | Hand delete the managed object |
| get() | Access the raw pointer (Use sparingly!) |
| deref | Use * or -> just like a raw pointer! |
| Release | Transfer ownership BACK to a raw pointer |
| Array | std::unique_ptr<int[]> handles arrays! |
| Count | ptr.use_count() (For shared pointers only!) |
| Expired | wptr.expired() (Check if weak object is gone!) |
| Lock | wptr.lock() (Convert weak to shared BEFORE use!) |
| Custom | You can provide your own "Deleter" logic! |
| Efficiency | Same size as a regular pointer (unique_ptr) |
| Security | Zero risk of "Use-after-free" errors |