C++: Difference between revisions

142 bytes added ,  7 October 2019
no edit summary
No edit summary
No edit summary
Line 71: Line 71:
===Memory===
===Memory===
==== Garbage Collection ====
==== Garbage Collection ====
[https://www.geeksforgeeks.org/auto_ptr-unique_ptr-shared_ptr-weak_ptr-2/ Smart Pointers]<br>
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>
<br>
Traditional C++ does not have garbage collection.<br>
Traditional C++ does not have garbage collection.<br>
After using <code>new</code> to allocate an object, use <code>delete</code> to deallocate it.  <br>
After using <code>new</code> to allocate an object, use <code>delete</code> to deallocate it.  <br>
You can also use C allocation with <code>malloc</code>, <code>calloc</code>, <code>alloca</code>, and <code>free</code>, though it is [https://stackoverflow.com/questions/184537/in-what-cases-do-i-use-malloc-and-or-new not recommended] since these are not type-safe.<br>
You can also use C allocation with <code>malloc</code>, <code>calloc</code>, <code>alloca</code>, and <code>free</code>, though it is [https://stackoverflow.com/questions/184537/in-what-cases-do-i-use-malloc-and-or-new not recommended] since these are not type-safe.<br>
In C++14, you can use [https://en.cppreference.com/w/cpp/memory/shared_ptr shared pointers] which does have automatic garbage collection.


==Programming Styles==
==Programming Styles==