C++: Difference between revisions

346 bytes added ,  7 October 2019
Line 70: Line 70:


===Memory===
===Memory===
==== Garbage Collection ====
====Smart Pointers===
[https://www.geeksforgeeks.org/smart-pointers-cpp/ Smart Pointers]<br>
[https://www.geeksforgeeks.org/auto_ptr-unique_ptr-shared_ptr-weak_ptr-2/ Smart Pointers]<br>
[https://www.geeksforgeeks.org/auto_ptr-unique_ptr-shared_ptr-weak_ptr-2/ Smart Pointers Types]<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 has automatic garbage collection.<br>
* <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>