Skip to content

Commit 4df4d1b

Browse files
committed
smart pointers basic information
1 parent aaf0ab3 commit 4df4d1b

1 file changed

Lines changed: 105 additions & 1 deletion

File tree

content/posts/cpp-for-cscharp.md

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,111 @@ int*ptr = &x;
104104

105105
**Smart:**
106106

107-
There are 3 types of smart pointers: ``std::unique_ptr`` ``std::shared_ptr`` ``std::weak_ptr``, to use them with ``#include <memory>``
107+
In modern idiomatic C++ there is a safer approach to pointers, called smart pointers.
108+
There are 3 types of smart pointers: ``std::unique_ptr`` ``std::shared_ptr`` ``std::weak_ptr``,
109+
to use them include the ``#include <memory>`` header in your code.
110+
Fundamentally what a smart pointer is, is a pointer that automatically frees the memory of whatever it points to when the pointer itself goes out of scope.
111+
112+
**Unique Pointer:**
113+
114+
This pointer has ownership over whatever it points to, if it goes out of scope and gets dropped of the stack, so does the memory it points to.
115+
116+
What makes this one different from the other smart pointers:
117+
118+
- there can only be only one unique pointer per object
119+
- the pointer and the object have the same lifetime
120+
121+
<br>
122+
123+
```cpp
124+
// cpp
125+
126+
#include <memory>
127+
128+
void test()
129+
{
130+
// making a unique pointer
131+
unique_ptr<Foo> foo_ptr = make_unique<Foo>(new Foo());
132+
133+
// No need to delete, gets freed at end of scope
134+
}
135+
```
136+
137+
**Shared Pointer:**
138+
139+
What makes this one different from the other smart pointers:
140+
141+
- there can be many shared pointers many per object
142+
- target object lives as long at least one share exists
143+
144+
<br>
145+
146+
```cpp
147+
// cpp
148+
149+
#include <memory>
150+
151+
void test()
152+
{
153+
// making a shared pointer
154+
shared_ptr<Foo> sp_1 = make_shared<Foo>(new Foo());
155+
156+
// sharing ownership
157+
shared_ptr<int> sp_2 = sp_1;
158+
159+
// get amount of shares
160+
int shares = sp_1.use_count() // there are now 2 shares
161+
162+
// you can manually drop a share like so
163+
sp_2.reset();
164+
165+
int shares = sp_1.use_count() // there is now 1 share
166+
167+
// at the end of this scope the first pointer also drops
168+
// now since there are no more shares the object is freed
169+
}
170+
```
171+
172+
**Weak Pointer:**
173+
174+
What makes this one different from the other smart pointers:
175+
176+
- weak pointers dont have any ownership
177+
- weak pointers can only be used with shared pointers, not unique pointers
178+
- there can be many weak pointers per object
179+
- weak pointers dont increase the amount of shares like a shared pointer
180+
- weak pointers dont own an object so the object's lifetime is not tied to the weak ptr
181+
- all weak pointers expire (points to nullptr) when the last shared ptr gets dropped
182+
183+
<br>
184+
185+
```cpp
186+
// cpp
187+
188+
#include <memory>
189+
190+
void test()
191+
{
192+
// making a shared pointer
193+
shared_ptr<Foo> sp = make_shared<Foo>(new Foo());
194+
195+
// sharing ownership but with weak pointer
196+
weak_ptr<Foo> wp = sp;
197+
198+
// get amount of shares
199+
int shares = sp.use_count() // still 1, weak ptr didnt add a share
200+
201+
// destroy the only and last shared pointer with ownership
202+
sp.reset();
203+
204+
// now since there are no more shares the object is freed
205+
206+
// the weak pointer like the object doesnt exist anymore
207+
bool does_object_exist = wp.expired();
208+
209+
// the weak pointer still exists but now points to nullptr
210+
}
211+
```
108212

109213
## Headers
110214

0 commit comments

Comments
 (0)