In C++, smart pointers let you focus on the problem at hand by freeing you from the worries of dealing with custom garbage collection techniques. Smart pointers let you use raw pointers safely. They take the responsibility of cleaning up the memory used by raw pointers.
C++ supports many types of smart pointers that can be used in different scenarios:
auto_ptr
unique_ptr
shared_ptr
weak_ptr
The auto_ptr
smart pointer was introduced in C++11. An auto_ptr
smart pointer helps release the heap memory automatically when it goes out of scope. However, due to the way auto_ptr
transfers ownership from one auto_ptr
instance to another, it was deprecated and unique_ptr
was introduced as its replacement. The shared_ptr
smart pointer helps multiple shared smart pointers reference the same object and takes care of the memory management burden. The weak_ptr
smart pointer helps resolve memory leak issues that arise due to the use of shared_ptr
when there is a cyclic dependency issue in the application...