Intro

Smart pointers are template classes in C++ that own and manage the lifetime of a dynamically allocated object. They automatically destroy the object when it’s no longer used, helping prevent memory leaks and dangling pointers.

  • Common types:

    • std::shared_ptr<T> – multiple owners; object is deleted when the last owner is gone.
    • std::unique_ptr<T> – single owner; object is deleted when that owner goes out of scope.
    • std::weak_ptr<T> – non-owning reference to a shared_ptr (prevents cycles).