Skip to content

Smart Pointers and RAII ​

Managing resources by hand (new/delete, fopen/fclose, lock/unlock) is the source of C++ programmers' nightmares. The RAII principle tells us: bind acquiring a resource to an object's construction, hand releasing it to the destructor, and let the scope manage the lifetime for you. In this chapter we first build a deep understanding of RAII, then set up the "ownership" model—exclusive, shared, and borrowed, each in its rightful place—then walk through the design philosophy and correct usage of unique_ptr, shared_ptr, and weak_ptr one by one, and finally see how custom deleters and scope_guard handle more complex resource scenarios.

Chapter Contents ​

pdf-latest-4-g85128cc · 85128cc · 2026-10-05