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: content/posts/cpp-for-cscharp.md
+105-1Lines changed: 105 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -104,7 +104,111 @@ int*ptr = &x;
104
104
105
105
**Smart:**
106
106
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
0 commit comments