GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||
GeezerVison is showing www.geeksforgeeks.org — Smart Pointers in C++ - GeeksforGeeks Smart Pointers in C++Last Updated : 31 Aug, 2026 Smart pointers are objects that manage dynamically allocated memory automatically. They are provided by the C++ Standard Library and help manage the lifetime of dynamically allocated objects.
Example: Problem with Raw Pointers (Memory Leak Example)
Output 10 Explanation:
Syntax of Smart PointersSmart pointers are template classes, so the type of object they manage is specified inside angle brackets.
For example:
Types of Smart PointersC++ provides three commonly used smart pointers: 1. auto_ptr (Deprecated)auto_ptr was an early smart pointer that automatically deleted the managed object when it went out of scope.
![]()
Output 10 10
2. unique_ptrunique_ptr stores one pointer only at a time. We cannot copy unique_ptr, only transfer ownership of the object to another unique_ptr using the move() method.
![]()
Output 50 50 Explanation: make_unique() creates a unique_ptr that exclusively owns the Rectangle object, while move() transfers ownership from ptr1 to ptr2. The object is automatically deleted when the owning unique_ptr goes out of scope. 3. shared_ptrshared_ptr allows multiple pointers to share ownership of the same object. It uses reference counting to manage memory.
![]()
Output 50 50 2 Explanation: make_shared() creates a shared_ptr, and copying ptr1 to ptr2 makes them share ownership. use_count() shows the number of owners, and the object is destroyed when the last shared_ptr is gone. 4. weak_ptrweak_ptr is a non-owning smart pointer used with shared_ptr. It provides access to an object without increasing its reference count. It is mainly used to prevent circular ownership between shared_ptr objects.
![]()
Output 50 1 Explanation: ptr1 owns the Rectangle object, while ptr2 observes it without increasing the reference count. lock() can be used to obtain a shared_ptr if the object is still alive. Problem: Circular Dependency with shared_ptrIf two objects hold shared_ptr to each other:
![]() This is the reason we use weak pointers(weak_ptr) is a non-owning reference to an object managed by shared_ptr. ![]() So, in the case of shared_ptr because of cyclic dependency use_count never reaches zero which is prevented by using weak_ptr, which removes this problem by declaring A_ptr as weak_ptr, thus class A does not own it, only has access to it and we also need to check the validity of object as it may go out of scope. In general, it is a design issue.
Output 50 1 Problems with Raw PointersRaw pointers provide direct control over memory but require careful manual management.
Pointers Vs Smart Pointers
Comment ExploreStandard Template Library(STL) Practice & Problems | ||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |