forked from pawanrajsingh2088/cpp-programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmart-pointers.cpp
More file actions
116 lines (98 loc) · 3.19 KB
/
smart-pointers.cpp
File metadata and controls
116 lines (98 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
* Program: Simple Smart Pointer Implementation
* Problem Statement: Implement a basic smart pointer class to demonstrate
* RAII (Resource Acquisition Is Initialization) in C++
*
* Input Example:
* SmartPtr<int> ptr = make_smart<int>(42);
* cout << *ptr << endl;
*
* Output Example:
* 42
* Memory automatically released when ptr goes out of scope
*
* Complexity: O(1) for all operations
* Features: Automatic memory management, Move semantics, Reference counting
*/
#include <iostream>
#include <utility>
template<typename T>
class SmartPtr {
private:
T* ptr;
size_t* ref_count;
public:
// Constructor
explicit SmartPtr(T* p = nullptr) : ptr(p), ref_count(new size_t(1)) {
if (ptr) {
std::cout << "SmartPtr created with value: " << *ptr << std::endl;
}
}
// Copy constructor
SmartPtr(const SmartPtr& other) : ptr(other.ptr), ref_count(other.ref_count) {
++(*ref_count);
std::cout << "SmartPtr copied, ref count: " << *ref_count << std::endl;
}
// Move constructor
SmartPtr(SmartPtr&& other) noexcept : ptr(other.ptr), ref_count(other.ref_count) {
other.ptr = nullptr;
other.ref_count = nullptr;
std::cout << "SmartPtr moved" << std::endl;
}
// Destructor
~SmartPtr() {
if (ref_count && --(*ref_count) == 0) {
delete ptr;
delete ref_count;
std::cout << "SmartPtr destroyed, memory released" << std::endl;
}
}
// Assignment operator
SmartPtr& operator=(const SmartPtr& other) {
if (this != &other) {
// Decrease current ref count
if (ref_count && --(*ref_count) == 0) {
delete ptr;
delete ref_count;
}
// Copy new values
ptr = other.ptr;
ref_count = other.ref_count;
++(*ref_count);
}
return *this;
}
// Dereference operators
T& operator*() const { return *ptr; }
T* operator->() const { return ptr; }
// Get raw pointer
T* get() const { return ptr; }
// Check if valid
bool is_valid() const { return ptr != nullptr; }
// Get reference count
size_t use_count() const { return ref_count ? *ref_count : 0; }
};
// Helper function to create SmartPtr
template<typename T, typename... Args>
SmartPtr<T> make_smart(Args&&... args) {
return SmartPtr<T>(new T(std::forward<Args>(args)...));
}
// Demo function
void demonstrateSmartPtr() {
std::cout << "\n=== Smart Pointer Demo ===" << std::endl;
// Create smart pointer
SmartPtr<int> ptr1 = make_smart<int>(100);
std::cout << "ptr1 value: " << *ptr1 << ", ref count: " << ptr1.use_count() << std::endl;
// Copy smart pointer
SmartPtr<int> ptr2 = ptr1;
std::cout << "ptr2 value: " << *ptr2 << ", ref count: " << ptr2.use_count() << std::endl;
// Move smart pointer
SmartPtr<int> ptr3 = std::move(ptr1);
std::cout << "ptr3 value: " << *ptr3 << ", ref count: " << ptr3.use_count() << std::endl;
std::cout << "=== End of scope ===" << std::endl;
}
int main() {
demonstrateSmartPtr();
std::cout << "Program ended" << std::endl;
return 0;
}