← Back to C++ Course | Chapter 14: Memory Management | Lesson 6 of 6

C++ shared_ptr & weak_ptr

shared_ptr Basics

std::shared_ptr allows multiple pointers to jointly own the same dynamically allocated object, tracked via an internal reference count. The object is only deleted once that count drops to zero, meaning the last shared_ptr to go out of scope is the one that actually frees the memory.

Example: shared_ptr Basics

cpp
#include <iostream>
#include <memory>

int main() {
	std::shared_ptr<int> ptr(new int(5));
	std::cout << *ptr << " count=" << ptr.use_count() << std::endl;
	return 0;
}

Copying shared_ptr

Unlike unique_ptr, a shared_ptr can be freely copied -- each copy increments the shared reference count, and each destroyed copy decrements it. This makes shared_ptr the right tool when an object's lifetime genuinely needs to be shared across multiple parts of a program, rather than owned by just one.

Example: Copying shared_ptr

cpp
#include <iostream>
#include <memory>

int main() {
	std::shared_ptr<int> ptr1(new int(5));
	std::shared_ptr<int> ptr2 = ptr1;
	std::cout << ptr1.use_count() << std::endl;
	return 0;
}

make_shared

std::make_shared<T>(args) allocates the object and its reference-counting control block together in a single heap allocation, making it both faster and more exception-safe than constructing a shared_ptr from a separately new'd raw pointer.

Example: make_shared

cpp
#include <iostream>
#include <memory>

int main() {
	auto ptr = std::make_shared<int>(42);
	std::cout << *ptr << std::endl;
	return 0;
}

The Circular Dependency Problem

If two objects hold shared_ptrs to each other, their reference counts can never reach zero -- each keeps the other alive indefinitely, even if nothing outside the pair references them anymore. This reference cycle is a real memory leak that shared_ptr alone cannot detect or resolve.

Example: The Circular Dependency Problem

cpp
#include <iostream>
#include <memory>

class Node {
public:
	std::shared_ptr<Node> next;
	~Node() { std::cout << "Node destroyed" << std::endl; }
};

int main() {
	auto a = std::make_shared<Node>();
	auto b = std::make_shared<Node>();
	a->next = b;
	b->next = a;
	std::cout << "Circular references created" << std::endl;
	return 0;
}

weak_ptr Basics

std::weak_ptr observes an object managed by a shared_ptr without incrementing its reference count, which is exactly how you break the circular-reference problem above. Because the object it points to might already be gone, you must call lock() to obtain a temporary shared_ptr before safely accessing it.

Example: weak_ptr Basics

cpp
#include <iostream>
#include <memory>

int main() {
	std::shared_ptr<int> shared = std::make_shared<int>(42);
	std::weak_ptr<int> weak = shared;
	std::cout << shared.use_count() << std::endl;
	return 0;
}
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.