You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: exercises/concept/power-of-troy/.docs/introduction.md
-27Lines changed: 0 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,6 @@ The two most commonly used smart pointers are `std::unique_ptr` and `std::shared
10
10
11
11
## Unique Pointers
12
12
13
-
14
13
`std::unique_ptr` is a smart pointer that owns the object exclusively.
15
14
It ensures that at any given time, only one `std::unique_ptr` object owns the resource.
16
15
When the owning `std::unique_ptr` is destroyed or reset, it automatically destructs the objects and releases its memory.
@@ -64,35 +63,9 @@ It combines memory allocation for the control block and the managed object into
64
63
Additionally, automatic deduction of template arguments simplifies code and enhances readability.
65
64
Using `std::make_shared()` promotes cleaner, safer, and more efficient code when working with `std::shared_ptr` objects in C++.
66
65
67
-
68
-
~~~~exercism/advanced
69
-
## Weak Pointers
70
-
71
-
`std::weak_ptr` is a companion class to `std::shared_ptr` that provides a non-owning "weak" reference to an object managed by a shared pointer.
72
-
73
-
```cpp
74
-
// Creating a shared pointer
75
-
auto your_account = std::make_shared<std::string>("secret_subscription_password");
76
-
// Creating a shared pointer that shares ownership
77
-
auto your_flatmates_account = your_account;
78
-
79
-
// Creating a weak pointer from the shared pointer
80
-
auto your_flatmates_boyfriends_account = your_flatmates_account;
81
-
// if your_account and your_flatmates_account are deleted, there is no more reference to the shared pointer.
82
-
// your_flatmates_boyfriends_account will be a null pointer and cannot use the associated object any longer.
83
-
```
84
-
85
-
Weak pointers are useful in scenarios where cyclic references need to be broken to prevent memory leaks.
86
-
`std::weak_ptr` was designed to address the issue of cyclic ownership, also known as circular references, that can occur when using `std::shared_ptr`.
87
-
In a cyclic ownership scenario, two or more `std::shared_ptr` objects are referencing each other, creating a cycle where none of the objects can be deleted because they have strong references to each other, leading to memory leaks.
88
-
`std::weak_ptr` provides a solution to this problem by allowing weak references to shared objects without contributing to their reference count.
89
-
This means that it can observe and access the shared object but doesn't prevent it from being deleted.
90
-
~~~~
91
-
92
66
## Usage advice
93
67
94
68
Use smart pointers by default: `std::unique_ptr` for exclusive ownership and `std::shared_ptr` for shared ownership.
95
69
Reserve raw pointers for non-owning references or when interfacing with legacy code.
96
70
In most cases, `std::unique_ptr` is sufficient for exclusive ownership, as it offers lightweight memory management without the overhead of reference counting.
97
71
`std::shared_ptr` should be used sparingly, as it introduces overhead and complexity unless true shared ownership is needed.
98
-
`std::weak_ptr` is specialized for breaking cyclic dependencies or observing shared objects, but it's not commonly used.
0 commit comments