← Back to C++ Course | Chapter 7: Pointers & References | Lesson 11 of 11

C++ Smart Pointers

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

cpp
#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

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

cpp
#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;
}

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

cpp
#include <iostream>
#include <memory>

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

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

cpp
#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;
}

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

cpp
#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 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.