#include <iostream>
#include <memory>
using namespace std;
class MyClass {
private:
int value;
public:
MyClass(int v) : value(v) {
cout << "MyClass object created with value: " << value << endl;
}
void display() {
cout << "Value: " << value << endl;
}
~MyClass() {
cout << "MyClass object destroyed (value: " << value << ")" << endl;
}
};
int main() {
cout << "=== unique_ptr ===" << endl;
{
// unique_ptr - exclusive ownership
unique_ptr<MyClass> ptr1 = make_unique<MyClass>(10);
ptr1->display();
// Cannot copy, but can move
unique_ptr<MyClass> ptr2 = move(ptr1);
if (ptr1 == nullptr) {
cout << "ptr1 is now null (ownership transferred)" << endl;
}
ptr2->display();
} // Automatically deleted
cout << "\n=== shared_ptr ===" << endl;
{
// shared_ptr - shared ownership
shared_ptr<MyClass> ptr1 = make_shared<MyClass>(20);
cout << "Reference count: " << ptr1.use_count() << endl;
{
shared_ptr<MyClass> ptr2 = ptr1; // Share ownership
cout << "Reference count: " << ptr1.use_count() << endl;
ptr2->display();
} // ptr2 goes out of scope
cout << "Reference count: " << ptr1.use_count() << endl;
ptr1->display();
} // Automatically deleted when count reaches 0
cout << "\nAll objects automatically destroyed" << endl;
return 0;
}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
This program teaches you how to use Smart Pointers in C++ (C++11). Smart pointers automatically manage memory, preventing memory leaks and making memory management safer and easier. They automatically delete objects when no longer needed, eliminating the need for manual delete calls.
1. What This Program Does
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.
2. Header Files Used
-
#include <iostream>
- Provides cout and cin for input/output operations.
-
#include <memory>
- Provides smart pointer classes (unique_ptr, shared_ptr).
3. 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
4. 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); // Use ptr } // ptr automatically deleted here
5. 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
6. 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
7. 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
8. 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
9. return 0;
This ends the program successfully.
Summary
- Smart pointers: automatically manage memory, prevent memory leaks.
- unique_ptr: exclusive ownership, automatically deleted when out of scope.
- shared_ptr: shared ownership with reference counting, deleted when last owner destroyed.
- No need for manual delete - automatic cleanup.
- Preferred over raw pointers in modern C++ (C++11+).
- Understanding smart pointers enables safe, automatic memory management.
- Essential for modern C++ programming and exception-safe code.
This program is fundamental for learning modern C++ memory management, understanding automatic resource management, and preparing for RAII (Resource Acquisition Is Initialization) patterns in C++ programs.