C++ Smart Pointers
In this page:
#include <memory>
std::unique_ptr<data_type> pointer_name = std::make_unique<data_type>(value);
std::shared_ptr<data_type> pointer_name = std::make_shared<data_type>(value);
Introduction to Smart Pointers
Smart pointers are wrapper classes that manage dynamically allocated heap memory automatically, releasing it the moment the smart pointer object itself goes out of scope.
This eliminates the need to call delete manually and is the modern C++ answer to manual memory-management bugs.
Example: Introduction to Smart Pointers
#include <iostream>
#include <memory>
int main() {
std::unique_ptr<int> ptr = std::make_unique<int>(10);
std::cout << *ptr << std::endl;
return 0;
} // memory released automatically here
Login to try C/C++/Java/PHP code in the editor
Unique Pointer
A unique_ptr owns its dynamically allocated resource exclusively — it cannot be copied, only moved — which guarantees at compile time that no two pointers will ever try to delete the same memory.
It's the default choice whenever an object has a single, clear owner.
Example: Unique Pointer
#include <iostream>
#include <memory>
int main() {
std::unique_ptr<int> ptr1 = std::make_unique<int>(5);
std::unique_ptr<int> ptr2 = std::move(ptr1); // ownership transferred, not copied
std::cout << *ptr2 << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Shared Pointer
A shared_ptr allows multiple pointers to jointly own the same memory block by keeping an internal reference count of how many shared_ptr instances currently point to it.
The memory is only freed once the very last shared_ptr referencing it is destroyed, making shared ownership safe.
Example: Shared Pointer
// Include std::cout and std::cin
#include <iostream>
// Include smart pointers
#include <memory>
// Program execution starts in main()
int main() {
// Use a shared_ptr: a reference-counted smart pointer
std::shared_ptr<int> ptr1 = std::make_shared<int>(5);
// Use a shared_ptr: a reference-counted smart pointer
std::shared_ptr<int> ptr2 = ptr1;
// Print to the console with cout
std::cout << "Use count: " << ptr1.use_count() << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Weak Pointer
A weak_ptr holds a non-owning reference to an object managed by a shared_ptr, meaning it doesn't affect the reference count.
This is essential for breaking circular references between shared_ptrs, which would otherwise keep each other's memory alive forever and cause a silent leak.
Example: Weak Pointer
#include <iostream>
#include <memory>
int main() {
std::shared_ptr<int> shared = std::make_shared<int>(5);
std::weak_ptr<int> weak = shared; // doesn't affect the reference count
std::cout << "Use count still: " << shared.use_count() << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Safe Factory Functions
To create smart pointers safely and efficiently, prefer the factory functions make_unique and make_shared over directly calling new.
They combine memory allocation and object construction into a single step, which reduces overhead and avoids a subtle exception-safety pitfall of separate new calls.
Example: Safe Factory Functions
#include <iostream>
#include <memory>
int main() {
auto ptr = std::make_unique<int>(42); // preferred over: new int(42)
std::cout << *ptr << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Using
newanddeletemanually while also storing the result in a smart pointer, causing a double free. - Copying a
unique_ptr, which is not allowed. - Creating two
shared_ptrfrom the same raw pointer.
- Pointers store memory addresses and are dereferenced to read or modify the value they point to.
- Pointer arithmetic, and the links between pointers, arrays, and functions, let you work with memory directly.
- References are aliases for variables, and smart pointers help manage memory safely.
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: