Smart Pointers in C++

Smart Pointers (unique_ptr, shared_ptr) in C++.

IntermediateMemory Management ProgramsExample 11 of 11
smart-pointers.cpp
1#include <iostream>
2#include <memory>
3using namespace std;
4
5class MyClass {
6private:
7 int value;
8
9public:
10 MyClass(int v) : value(v) {
11 cout << "MyClass object created with value: " << value << endl;
12 }
13
14 void display() {
15 cout << "Value: " << value << endl;
16 }
17
18 ~MyClass() {
19 cout << "MyClass object destroyed (value: " << value << ")" << endl;
20 }
21};
22
23int main() {
24 cout << "=== unique_ptr ===" << endl;
25 {
26 // unique_ptr - exclusive ownership
27 unique_ptr<MyClass> ptr1 = make_unique<MyClass>(10);
28 ptr1->display();
29
30 // Cannot copy, but can move
31 unique_ptr<MyClass> ptr2 = move(ptr1);
32 if (ptr1 == nullptr) {
33 cout << "ptr1 is now null (ownership transferred)" << endl;
34 }
35 ptr2->display();
36 } // Automatically deleted
37
38 cout << "\n=== shared_ptr ===" << endl;
39 {
40 // shared_ptr - shared ownership
41 shared_ptr<MyClass> ptr1 = make_shared<MyClass>(20);
42 cout << "Reference count: " << ptr1.use_count() << endl;
43
44 {
45 shared_ptr<MyClass> ptr2 = ptr1; // Share ownership
46 cout << "Reference count: " << ptr1.use_count() << endl;
47 ptr2->display();
48 } // ptr2 goes out of scope
49
50 cout << "Reference count: " << ptr1.use_count() << endl;
51 ptr1->display();
52 } // Automatically deleted when count reaches 0
53
54 cout << "\nAll objects automatically destroyed" << endl;
55
56 return 0;
57}

Output

=== unique_ptr ===
MyClass object created with value: 10
Value: 10
ptr1 is now null (ownership transferred)
Value: 10
MyClass object destroyed (value: 10)

=== shared_ptr ===
MyClass object created with value: 20
Reference count: 1
Reference count: 2
Value: 20
Reference count: 1
Value: 20
MyClass object destroyed (value: 20)

All objects automatically destroyed

What's going on

The program demonstrates smart pointer usage:

unique_ptr: exclusive ownership, automatically deletes when out of scope
shared_ptr: shared ownership with reference counting
Automatic memory management
No need for manual delete

Smart pointers make memory management automatic and safe.

Header Files Used

1.#include <iostream>
Provides cout and cin for input/output operations.
2.#include <memory>
Provides smart pointer classes (unique_ptr, shared_ptr).

Understanding Smart Pointers

Smart Pointer Concept

:

Automatically manage memory
Delete objects when no longer needed
Prevent memory leaks
Safer than raw pointers

Types

:

unique_ptr: exclusive ownership
shared_ptr: shared ownership
weak_ptr: non-owning reference

unique_ptr - Exclusive Ownership

Basic Usage

:

unique_ptr<int> ptr = make_unique<int>(10);

How it works

:

Only one unique_ptr can own object
Automatically deleted when unique_ptr destroyed
Cannot be copied (only moved)
Exclusive ownership model

Automatic Cleanup

:

{

unique_ptr<int> ptr = make_unique<int>(10);

} // ptr automatically deleted here

1// Use ptr

shared_ptr - Shared Ownership

Basic Usage

:

shared_ptr<int> ptr1 = make_shared<int>(10);

shared_ptr<int> ptr2 = ptr1; // Share ownership

How it works

:

Multiple shared_ptr can own same object
Reference counting tracks owners
Deleted when last shared_ptr destroyed
Shared ownership model

Reference Counting

:

Each shared_ptr increments count
When shared_ptr destroyed, count decrements
Object deleted when count reaches 0

When to Use Smart Pointers

Best For

:

Automatic memory management
Preventing memory leaks
Modern C++ code (C++11+)
Replacing raw pointers
Exception-safe code

Example Scenarios

:

Dynamic object creation
Resource management
Container of pointers
Polymorphism with base pointers
Any dynamic memory allocation

Advantages Over Raw Pointers

Automatic Cleanup

:

No need for manual delete
Automatically freed when out of scope
Exception-safe (freed even if exception occurs)

Memory Safety

:

Prevents memory leaks
Prevents double deletion
Prevents dangling pointers

Modern C++

:

Preferred over raw pointers
Part of standard library
Type-safe and efficient

Important Considerations

unique_ptr

:

Exclusive ownership
Cannot be copied
Can be moved (transfer ownership)
Lightweight (no overhead)

shared_ptr

:

Shared ownership
Can be copied
Reference counting overhead
Thread-safe reference counting

Performance

:

unique_ptr: no overhead
shared_ptr: slight overhead (reference counting)
Usually negligible for most applications

return 0;

This ends the program successfully.