Skip to content

Commit f7f8795

Browse files
committed
fixup! feat: add concept ex for smart pointers
1 parent 67cdd3c commit f7f8795

File tree

1 file changed

+0
-27
lines changed

1 file changed

+0
-27
lines changed

exercises/concept/power-of-troy/.docs/introduction.md

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ The two most commonly used smart pointers are `std::unique_ptr` and `std::shared
1010

1111
## Unique Pointers
1212

13-
1413
`std::unique_ptr` is a smart pointer that owns the object exclusively.
1514
It ensures that at any given time, only one `std::unique_ptr` object owns the resource.
1615
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
6463
Additionally, automatic deduction of template arguments simplifies code and enhances readability.
6564
Using `std::make_shared()` promotes cleaner, safer, and more efficient code when working with `std::shared_ptr` objects in C++.
6665

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-
9266
## Usage advice
9367

9468
Use smart pointers by default: `std::unique_ptr` for exclusive ownership and `std::shared_ptr` for shared ownership.
9569
Reserve raw pointers for non-owning references or when interfacing with legacy code.
9670
In most cases, `std::unique_ptr` is sufficient for exclusive ownership, as it offers lightweight memory management without the overhead of reference counting.
9771
`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

Comments
 (0)