cpp-smart-pointers

Manage C++ resource ownership with unique_ptr, shared_ptr, and weak_ptr.

187|20|Updated Nov 20, 2025
One-click install
npx skills add https://github.com/TheBushidoCollective/han --skill cpp-smart-pointers
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: cpp-smart-pointers
Source: https://github.com/TheBushidoCollective/han/tree/main/jutsu/jutsu-cpp/cpp-smart-pointers
Command: npx skills add https://github.com/TheBushidoCollective/han --skill cpp-smart-pointers

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill demonstrates safe memory management in C++ using smart pointers and RAII.

Core Features & Use Cases

  • unique_ptr & shared_ptr: Ownership semantics and lifetime management.
  • Custom Deleters: Custom cleanup logic for resources.
  • Resource Wrappers: Safe wrappers around raw resource handles.

Quick Start

Create a simple class that manages a file handle with a unique_ptr and a custom deleter.

Frequently Asked Questions about cpp-smart-pointers

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do smart pointers prevent memory leaks in C++?

Smart pointers automate memory management through RAII, automatically deallocating resources when they go out of scope. unique_ptr provides exclusive ownership with guaranteed cleanup, while shared_ptr uses reference counting to free memory only when the last owner releases it, eliminating manual delete calls and dangling pointers.

When should I use unique_ptr versus shared_ptr?

Use unique_ptr for exclusive ownership where only one object manages a resource's lifetime—it's lighter and faster. Use shared_ptr when multiple parts of code need concurrent access to the same resource; reference counting tracks ownership and deletes only when all references cease.

Can I use custom deleters with smart pointers?

Yes. Custom deleters let you define resource-specific cleanup logic for unique_ptr and shared_ptr. This is essential for non-heap resources like file handles, database connections, or OS resources that require special finalization before deallocation.

What is weak_ptr and why do I need it?

weak_ptr is a non-owning observer of shared_ptr objects that doesn't increment the reference count. It breaks circular reference cycles—where two shared_ptrs reference each other—preventing memory leaks when shared ownership creates deadlock patterns.

How do smart pointers work with move semantics?

Smart pointers support move semantics, transferring resource ownership without copying overhead. Moving a unique_ptr transfers exclusive ownership; moving a shared_ptr transfers the shared reference. This enables efficient resource handoff between functions and containers.