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

C++ Smart Pointers

Smart pointers are pointers that clean up after themselves when nobody needs the thing they point to. It is like a borrowed book that returns itself to the library.
Syntax
cpp
#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

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

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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using new and delete manually while also storing the result in a smart pointer, causing a double free.
  2. Copying a unique_ptr, which is not allowed.
  3. Creating two shared_ptr from the same raw pointer.
Chapter Summary
  • 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.

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.