-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCpp2_5.html
More file actions
79 lines (70 loc) · 3.26 KB
/
Copy pathCpp2_5.html
File metadata and controls
79 lines (70 loc) · 3.26 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
<article>
<h1>C++ II: Page 5 - Modern C++: Smart Pointers</h1>
<section>
<h2>Automating Memory Management</h2>
<p>Modern C++ (C++11 and later) introduced smart pointers to automate memory management, eliminating manual <code>new</code> and <code>delete</code> calls and drastically reducing memory leaks.</p>
<h3>The Code implementation</h3>
<pre><code class="language-cpp">/*
* File: smart_pointers.cpp
* Demonstration of std::unique_ptr, std::shared_ptr, and std::weak_ptr
*/
#include <iostream>
#include <memory> // For smart pointers
#include <string>
using namespace std;
class Resource {
public:
Resource() { cout << "Resource acquired." << endl; }
~Resource() { cout << "Resource released." << endl; }
void use() { cout << "Resource in use." << endl; }
};
int main() {
// 1. unique_ptr: Exclusive ownership
{
unique_ptr<Resource> uPtr = make_unique<Resource>();
uPtr->use();
} // Resource automatically released here
// 2. shared_ptr: Shared ownership
shared_ptr<Resource> sPtr1 = make_shared<Resource>();
{
shared_ptr<Resource> sPtr2 = sPtr1; // Ref count = 2
cout << "Ref count: " << sPtr1.use_count() << endl;
} // Ref count = 1
// 3. weak_ptr: Observer (prevents circular dependencies)
weak_ptr<Resource> wPtr = sPtr1;
if (auto shared = wPtr.lock()) {
cout << "Weak pointer still valid." << endl;
} else {
cout << "Weak pointer expired." << endl;
}
return 0;
}
/*
* Smart Pointer Nuances:
* 1. unique_ptr: Non-copyable, only movable. Ensures single owner.
* 2. shared_ptr: Copyable. Resource released when last reference is destroyed.
* 3. make_unique / make_shared: Preferred for exception safety and performance.
* 4. Use 'unique_ptr' by default; use 'shared_ptr' only if shared ownership is needed.
* 5. Weak pointers ('weak_ptr') can prevent circular dependencies in shared_ptr
* scenarios, where two objects reference each other.
* 6. Never call 'delete' on memory managed by smart pointers; this causes a double-free crash.
*/
</code></pre>
<div class="interactive-sim">
<h3>Smart Pointer Simulator</h3>
<button class="sim-button" onclick="
const out = document.getElementById('sim-output-cpp2_5');
out.innerHTML = 'unique_ptr<Res> ptr = make_unique<Res>();<br>';
setTimeout(() => out.innerHTML += '>> Resource acquired!<br>>> Out of scope...', 800);
setTimeout(() => out.innerHTML += '>> Resource released!', 1600);
">Run Simulation</button>
<div id="sim-output-cpp2_5" class="sim-output"></div>
</div>
</section>
<footer style="margin-top: 40px; padding: 20px; background: #f8f9fa; border-top: 1px solid #dee2e6;">
<div style="display: flex; justify-content: space-between;">
<a href="#" data-file="Cpp2_4.html" style="text-decoration: none; color: #6c757d;">← Previous: STL: Algorithms</a>
<a href="#" data-file="Cpp2_6.html" style="font-weight: bold; text-decoration: none; color: #007bff;">Next: Modern C++: Move Semantics →</a>
</div>
</footer>
</article>