In C++, smart pointers are a feature provided by the Standard Library to manage dynamically allocated memory automatically. They help prevent common issues like memory leaks and dangling pointers by ensuring that memory is automatically freed when it is no longer needed. The C++ Standard Library provides three primary smart pointers:
- std::unique_ptr
- std::shared_ptr
- std::weak_ptr
Each of these smart pointers serves a different purpose and has distinct characteristics.
std::unique_ptr
std::unique_ptr
can own a resource at any time.std::unique_ptr
goes out of scope.#include <iostream>
#include <memory>
class MyClass {
public:
MyClass() { std::cout << "MyClass Constructor" << std::endl; }
~MyClass() { std::cout << "MyClass Destructor" << std::endl; }
};
int main() {
std::unique_ptr<MyClass> ptr1 = std::make_unique<MyClass>();
// std::unique_ptr<MyClass> ptr2 = ptr1; // Error: Cannot copy unique_ptr
std::unique_ptr<MyClass> ptr2 = std::move(ptr1); // OK: Move ownership
return 0;
}
std::shared_ptr
std::shared_ptr
instances can own the same resource.std::shared_ptr
pointing to it is destroyed.std::shared_ptr
uses reference counting to keep track of how many shared_ptrs
are pointing to the resource.#include <iostream>
#include <memory>
class MyClass {
public:
MyClass() { std::cout << "MyClass Constructor" << std::endl; }
~MyClass() { std::cout << "MyClass Destructor" << std::endl; }
};
void useSharedPtr(std::shared_ptr<MyClass> ptr) {
std::cout << "Using shared_ptr" << std::endl;
}
int main() {
std::shared_ptr<MyClass> ptr1 = std::make_shared<MyClass>();
std::shared_ptr<MyClass> ptr2 = ptr1; // Shared ownership
useSharedPtr(ptr1);
std::cout << "Reference count: " << ptr1.use_count() << std::endl; // Outputs: 2
return 0;
}
std::weak_ptr
std::shared_ptr
.std::shared_ptr
instances refer to each other, potentially causing a memory leak.std::shared_ptr
using the lock()
method, which returns a std::shared_ptr
if the resource still exists.#include <iostream>
#include <memory>
class MyClass {
public:
MyClass() { std::cout << "MyClass Constructor" << std::endl; }
~MyClass() { std::cout << "MyClass Destructor" << std::endl; }
};
int main() {
std::shared_ptr<MyClass> sharedPtr = std::make_shared<MyClass>();
std::weak_ptr<MyClass> weakPtr = sharedPtr; // Weak reference to sharedPtr
if (std::shared_ptr<MyClass> lockedPtr = weakPtr.lock()) {
std::cout << "Object is still alive" << std::endl;
} else {
std::cout << "Object has been destroyed" << std::endl;
}
return 0;
}
std::unique_ptr:
std::shared_ptr:
std::weak_ptr:
std::shared_ptr
to break circular references, which can prevent memory from being freed.std::unique_ptr
: Exclusive ownership, non-copyable, moveable.std::shared_ptr
: Shared ownership, reference-counted, safe sharing.std::weak_ptr
: Non-owning, used to avoid circular references, requires conversion to shared_ptr for access.These smart pointers are fundamental tools in modern C++ for managing dynamically allocated resources safely and efficiently.
Previous Page | Course Schedule | Course Content