Smart Pointers
Smart Pointers (unique_ptr, shared_ptr) in C++
C++ Smart Pointers Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#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;
}=== 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
Understanding Smart Pointers
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:
Smart pointers make memory management automatic and safe.
---
2. Header Files Used
---
3. Understanding Smart Pointers
Smart Pointer Concept
:
Types
:
---
4. unique_ptr - Exclusive Ownership
Basic Usage
:
unique_ptr<int> ptr = make_unique<int>(10);
How it works
:
Automatic Cleanup
:
{
unique_ptr<int> ptr = make_unique<int>(10);
} // ptr automatically deleted here
---
// Use ptr5. shared_ptr - Shared Ownership
Basic Usage
:
shared_ptr<int> ptr1 = make_shared<int>(10);
shared_ptr<int> ptr2 = ptr1; // Share ownership
How it works
:
Reference Counting
:
---
6. When to Use Smart Pointers
Best For
:
Example Scenarios
:
---
7. Advantages Over Raw Pointers
Automatic Cleanup
:
Memory Safety
:
Modern C++
:
---
8. Important Considerations
unique_ptr
:
shared_ptr
:
Performance
:
---
9. return 0;
This ends the program successfully.
---
Summary
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.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
Practical Learning Notes for Smart Pointers
This C++ program is part of the "Memory Management Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.