C++ Shared Pointers
A shared pointer is a smart pointer that shares ownership of an object with other shared pointers. The object is deleted when the last shared pointer that owns it is dropped.
The count rises and falls
One shared pointer owns the value at first. A copy inside the inner scope raises the count to 2, and the end of that scope drops it back to 1.
#include <iostream>
#include <memory>
int main() {
auto first = std::make_shared<int>(7);
std::cout << "Count: " << first.use_count() << "\n";
{
auto second = first;
std::cout << "Count: " << first.use_count() << "\n";
std::cout << "Value: " << *second << "\n";
}
std::cout << "Count: " << first.use_count() << "\n";
return 0;
}Deletion waits for the last owner
The inner copy drops at the end of its scope and the object still lives. The destructor line prints only when reset drops the final owner.
#include <iostream>
#include <memory>
class Tracked {
public:
~Tracked() {
std::cout << "Tracked deleted\n";
}
};
int main() {
auto first = std::make_shared<Tracked>();
{
auto second = first;
std::cout << "Inside the inner scope\n";
}
std::cout << "Inner owner dropped\n";
first.reset();
std::cout << "All owners dropped\n";
return 0;
}An owner returned from a function
The factory function builds the value and returns its shared pointer. The value prints in main after the factory has ended.
#include <iostream>
#include <memory>
std::shared_ptr<int> makeValue() {
auto local = std::make_shared<int>(99);
return local;
}
int main() {
auto kept = makeValue();
std::cout << "Value: " << *kept << "\n";
std::cout << "Count: " << kept.use_count() << "\n";
return 0;
}Example
#include <iostream>
#include <memory>
class Settings {
public:
int level;
Settings() {
level = 8;
}
};
void showFirst(std::shared_ptr<Settings> settings) {
std::cout << "First reads level: " << settings->level << "\n";
}
void showSecond(std::shared_ptr<Settings> settings) {
std::cout << "Second reads level: " << settings->level << "\n";
}
int main() {
auto settings = std::make_shared<Settings>();
showFirst(settings);
showSecond(settings);
std::cout << "Owners in main: " << settings.use_count() << "\n";
return 0;
}Key points
- The <memory> header provides std::shared_ptr and std::make_shared.
- std::make_shared builds an object and returns a shared pointer that owns it.
- Copying a shared pointer adds one more owner instead of copying the object.
- use_count reports how many shared pointers own the object.
- The object is deleted when its last owner is dropped.
- Passing a shared pointer by value copies the pointer, not the object.
In this example, one Settings object is built in main with its level set to 8. Two helper functions each receive a copy of the shared pointer and read the same level. Back in main, the count reads 1 because the helper copies are gone.
Note
A shared pointer fits a value that several parts of a program must keep alive.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
Two shared pointers own one object. What does use_count report?