Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are two widely-used memory allocation techniques: automatic allocation and dynamic allocation. Commonly, there is a corresponding region of memory for each: the stack and the heap.</p> <h3>Stack</h3> <p>The stack always allocates memory in a sequential fashion. It can do so because it requires you to release the memory in the reverse order (First-In, Last-Out: FILO). This is the memory allocation technique for local variables in many programming languages. It is very, very fast because it requires minimal bookkeeping and the next address to allocate is implicit.</p> <p>In C++, this is called <em>automatic storage</em> because the storage is claimed automatically at the end of scope. As soon as execution of current code block (delimited using <code>{}</code>) is completed, memory for all variables in that block is automatically collected. This is also the moment where <em>destructors</em> are invoked to clean up resources.</p> <h3>Heap</h3> <p>The heap allows for a more flexible memory allocation mode. Bookkeeping is more complex and allocation is slower. Because there is no implicit release point, you must release the memory manually, using <code>delete</code> or <code>delete[]</code> (<code>free</code> in C). However, the absence of an implicit release point is the key to the heap's flexibility.</p> <h3>Reasons to use dynamic allocation</h3> <p>Even if using the heap is slower and potentially leads to memory leaks or memory fragmentation, there are perfectly good use cases for dynamic allocation, as it's less limited.</p> <p>Two key reasons to use dynamic allocation:</p> <ul> <li><p>You don't know how much memory you need at compile time. For instance, when reading a text file into a string, you usually don't know what size the file has, so you can't decide how much memory to allocate until you run the program.</p></li> <li><p>You want to allocate memory which will persist after leaving the current block. For instance, you may want to write a function <code>string readfile(string path)</code> that returns the contents of a file. In this case, even if the stack could hold the entire file contents, you could not return from a function and keep the allocated memory block.</p></li> </ul> <h3>Why dynamic allocation is often unnecessary</h3> <p>In C++ there's a neat construct called a <em>destructor</em>. This mechanism allows you to manage resources by aligning the lifetime of the resource with the lifetime of a variable. This technique is called <a href="http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization">RAII</a> and is the distinguishing point of C++. It "wraps" resources into objects. <code>std::string</code> is a perfect example. This snippet:</p> <pre><code>int main ( int argc, char* argv[] ) { std::string program(argv[0]); } </code></pre> <p>actually allocates a variable amount of memory. The <code>std::string</code> object allocates memory using the heap and releases it in its destructor. In this case, you did <em>not</em> need to manually manage any resources and still got the benefits of dynamic memory allocation.</p> <p>In particular, it implies that in this snippet:</p> <pre><code>int main ( int argc, char* argv[] ) { std::string * program = new std::string(argv[0]); // Bad! delete program; } </code></pre> <p>there is unneeded dynamic memory allocation. The program requires more typing (!) and introduces the risk of forgetting to deallocate the memory. It does this with no apparent benefit.</p> <h3>Why you should use automatic storage as often as possible</h3> <p>Basically, the last paragraph sums it up. Using automatic storage as often as possible makes your programs:</p> <ul> <li>faster to type;</li> <li>faster when run;</li> <li>less prone to memory/resource leaks.</li> </ul> <h3>Bonus points</h3> <p>In the referenced question, there are additional concerns. In particular, the following class:</p> <pre><code>class Line { public: Line(); ~Line(); std::string* mString; }; Line::Line() { mString = new std::string("foo_bar"); } Line::~Line() { delete mString; } </code></pre> <p>Is actually a lot more risky to use than the following one:</p> <pre><code>class Line { public: Line(); std::string mString; }; Line::Line() { mString = "foo_bar"; // note: there is a cleaner way to write this. } </code></pre> <p>The reason is that <code>std::string</code> properly defines a copy constructor. Consider the following program:</p> <pre><code>int main () { Line l1; Line l2 = l1; } </code></pre> <p>Using the original version, this program will likely crash, as it uses <code>delete</code> on the same string twice. Using the modified version, each <code>Line</code> instance will own its own string <em>instance</em>, each with its own memory and both will be released at the end of the program.</p> <h3>Other notes</h3> <p>Extensive use of <a href="http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization">RAII</a> is considered a best practice in C++ because of all the reasons above. However, there is an additional benefit which is not immediately obvious. Basically, it's better than the sum of its parts. The whole mechanism <em>composes</em>. It scales.</p> <p>If you use the <code>Line</code> class as a building block:</p> <pre><code> class Table { Line borders[4]; }; </code></pre> <p>Then</p> <pre><code> int main () { Table table; } </code></pre> <p>allocates four <code>std::string</code> instances, four <code>Line</code> instances, one <code>Table</code> instance and all the string's contents and <em>everything is freed automagically</em>.</p>
 

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