C++: Difference between revisions

Line 71: Line 71:
===Memory===
===Memory===
==== Garbage Collection ====
==== Garbage Collection ====
Traditional C++ does not have garbage collection.
Traditional C++ does not have garbage collection.<br>
After using <code>new</code> to allocate an object, use <code>delete</code> to deallocate it.   
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.
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.
In C++14, you can use [https://en.cppreference.com/w/cpp/memory/shared_ptr shared pointers] which does have automatic garbage collection.