Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>Memory Management</h2> <h3>Basics</h3> <ul> <li>Every 'use of' new must be matched by 'use of' delete</li> <li>Array new is different form normal new and has its own delete</li> </ul> <p>_</p> <pre><code> int* data1 = new int(5); delete data1; int* data2 = new int[5]; delete [] data2; </code></pre> <h3>Must Know</h3> <ul> <li>Exceptions</li> <li>RAII</li> <li>Rule of 4.</li> </ul> <p><a href="https://stackoverflow.com/questions/130117/throwing-exceptions-out-of-a-destructor/130123#130123">throwing exceptions out of a destructor</a><br> <a href="https://stackoverflow.com/questions/255612/c-dynamically-allocating-an-array-of-objects/255744#255744">Dynamically allocating an array of objects</a> <a href="https://stackoverflow.com/questions/1846144/1846409#1846409">Pattern name for create in constructor, delete in destructor (C++)</a></p> <h3>Best Practice</h3> <ul> <li>Never actually use RAW pointers.</li> <li>Always wrap pointers in Smart Pointers.</li> <li>Learn the different types of smart pointers and when to use each</li> </ul> <p><a href="https://stackoverflow.com/questions/94227/smart-pointers-or-who-owns-you-baby">Smart Pointers: Or who owns you baby?</a></p> <h3>Advanced:</h3> <ul> <li>Understanding Exception Guarantees</li> <li>Understanding the use of throw clause</li> </ul> <p><a href="https://stackoverflow.com/questions/106586/what-are-the-principles-guiding-your-exception-handling-policy/106749#106749">What are the principles guiding your exception handling policy?</a></p> <h2>Common ways to leak</h2> <h3>Basics</h3> <pre><code>// Every new is matched by a delete. for(int loop = 0;loop &lt; 10;++loop) { data = new int(5); } delete data; // The problem is that every 'use of' new is not matched by a delete. // Here we allocate 10 integers but only release the last one. </code></pre> <h3>Must Know</h3> <pre><code>class MyArray { // Use RAII to manage the dynamic array in an exception safe manor. public: MyArray(int size) :data( new int[size]) {} ~MyArray() { delete [] data; } // PROBLEM: // Ignored the rule of 4. // The compiler will generate a copy constructor and assignment operator. // These default compiler generated methods just copy the pointer. This will // lead to double deletes on the memory. private: int* data; }; </code></pre> <h3>Best Practice</h3> <pre><code>// Understand what the properties of the smart pointers are: // std::vector&lt;std::auto_ptr&lt;int&gt; &gt; data; // Will not work. You can't put auto_ptr into a standard container. // This is because it uses move semantics not copy semantics. </code></pre> <h3>Advanced:</h3> <pre><code>// Gurantee that exceptions don't screw up your object: // class MyArray { // ... As Above: Plus void resize(int newSize) { delete [] data; data = new int[newSize]; // What happens if this new throws (because there is not enough memory)? // You have deleted the old data so the old data so it points at invalid memory. // The exception will leave the object in a completely invalid state } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload