Use std::unique_ptr, std::shared_ptr, and std::weak_ptr for automatic memory management.
Smart pointers are a modern C++ feature (introduced in C++11) designed to automate the process of memory management and prevent common errors like memory leaks and dangling pointers. They are wrapper classes that store a raw pointer and ensure that the memory it points to is properly deallocated when the smart pointer object goes out of scope. This is an application of the RAII (Resource Acquisition Is Initialization) idiom. There are three main types of smart pointers in the `<memory>` header. **`std::unique_ptr`** represents exclusive ownership. Only one `unique_ptr` can point to a given object at any time. It's very lightweight and has virtually no performance overhead compared to a raw pointer. You cannot copy a `unique_ptr`, but you can move it, transferring ownership to another `unique_ptr`. **`std::shared_ptr`** allows for shared ownership. Multiple `shared_ptr` instances can point to the same object. It keeps a reference count of how many `shared_ptr`s are pointing to the object. When the last `shared_ptr` owning the object is destroyed, the managed memory is automatically deleted. **`std::weak_ptr`** is a companion to `shared_ptr`. It holds a non-owning ('weak') reference to an object managed by a `shared_ptr`. It's used to break circular references between `shared_ptr` instances, which would otherwise prevent the reference count from ever reaching zero, causing a memory leak. In modern C++, you should almost always prefer using smart pointers over raw pointers for owning memory.