5,322
edits
(→Memory) |
|||
Line 70: | Line 70: | ||
===Memory=== | ===Memory=== | ||
==== | ====Smart Pointers=== | ||
[https://www.geeksforgeeks.org/ | [https://www.geeksforgeeks.org/auto_ptr-unique_ptr-shared_ptr-weak_ptr-2/ Smart Pointers]<br> | ||
There are 4 types of smart pointers | |||
Starting from C++14, you should use smart pointers such as [https://en.cppreference.com/w/cpp/memory/shared_ptr <code>shared_ptr</code>] which | * <code>auto_ptr</code> which is deprecated | ||
* <code>unique_ptr</code> | |||
* <code>shared_ptr</code> | |||
* <code>weak_ptr</code> | |||
Use <code>unique_ptr</code> for ownership models.<br> | |||
Use <code>shared_ptr</code> when multiple objects need to reference the same thing.<br> | |||
Use <code>weak_ptr</code> to avoid cyclic dependencies which cause issues with reference counting.<br> | |||
====Garbage Collection==== | |||
Starting from C++14, you should use smart pointers such as [https://en.cppreference.com/w/cpp/memory/shared_ptr <code>shared_ptr</code>] which have automatic garbage collection.<br> | |||
<br> | <br> | ||
Traditional C++ does not have garbage collection.<br> | Traditional C++ does not have garbage collection.<br> |