<p>In my browsings amongst the Internet, I came across <a href="http://www.reddit.com/r/programming/comments/6y6lr/ask_proggit_which_is_more_useful_to_know_c_or_java/" rel="nofollow noreferrer">this post</a>, which includes this</p>

<blockquote>
  <p>"(Well written) C++ goes to great
  lengths to make stack automatic
  objects work "just like" primitives,
  as reflected in Stroustrup's advice to
  "do as the ints do". This requires a
  much greater adherence to the
  principles of Object Oriented
  development: your class isn't right
  until it "works like" an int,
  following the "Rule of Three" that
  guarantees it can (just like an int)
  be created, copied, and correctly
  destroyed as a stack automatic."</p>
</blockquote>

<p>I've done a little C, and C++ code, but just in passing, never anything serious, but I'm just curious, what it means exactly?</p>

<p>Can someone give an example?</p>

[{'answer_id': 30125, 'author': 'Brad Barker', 'author_id': 12081, 'author_profile': 'https://Stackoverflow.com/users/12081', 'pm_score': 1, 'selected': False, 'text': '<p>Variables in C++ can either be declared on the stack or the heap. When you declare a variable in C++, it automatically goes onto the stack, unless you explicitly use the new operator (it goes onto the heap).</p>\n\n<pre><code>MyObject x = MyObject(params); // onto the stack\n\nMyObject * y = new MyObject(params); // onto the heap\n</code></pre>\n\n<p>This makes a big difference in the way the memory is managed. When a variable is declared on the stack, it will be deallocated when it goes out of scope. A variable on the heap will not be destroyed until delete is explicitly called on the object.</p>\n'}, {'answer_id': 30139, 'author': 'Danail Nachev', 'author_id': 3219, 'author_profile': 'https://Stackoverflow.com/users/3219', 'pm_score': 1, 'selected': False, 'text': '<p>Stack automatic are variables which are allocated on the stack of the current method. The idea behind designing a class which can acts as Stack automatic is that it should be possible to fully initialize it with one call and destroy it with another. It is essential that the destructor frees all resources allocated by the object and its constructor returns an object which has been fully initialized and ready for use. Similarly for the copy operation - the class should be able to be easily made copies, which are fully functional and independent.</p>\n\n<p>The usage of such class should be similar to how primitive int, float, etc. are used. You define them (eventually give them some initial value) and then pass them around and in the end leave the compiler to the cleaning.</p>\n'}, {'answer_id': 30186, 'author': 'Christopher', 'author_id': 3186, 'author_profile': 'https://Stackoverflow.com/users/3186', 'pm_score': 5, 'selected': True, 'text': '<p>Stack objects are handled automatically by the compiler.</p>\n\n<p>When the scope is left, it is deleted.</p>\n\n<pre><code>{\n   obj a;\n} // a is destroyed here\n</code></pre>\n\n<p>When you do the same with a \'newed\' object you get a memory leak :</p>\n\n<pre><code>{\n    obj* b = new obj;\n}\n</code></pre>\n\n<p>b is not destroyed, so we lost the ability to reclaim the memory b owns. And maybe worse, the object cannot clean itself up.</p>\n\n<p>In C the following is common :</p>\n\n<pre><code>{\n   FILE* pF = fopen( ... );\n   // ... do sth with pF\n   fclose( pF );\n}\n</code></pre>\n\n<p>In C++ we write this :</p>\n\n<pre><code>{\n   std::fstream f( ... );\n   // do sth with f\n} // here f gets auto magically destroyed and the destructor frees the file\n</code></pre>\n\n<p>When we forget to call fclose in the C sample the file is not closed and may not be used by other programs. (e.g. it cannot be deleted).</p>\n\n<p>Another example, demonstrating the object string, which can be constructed, assigned to  and which is destroyed on exiting the scope.</p>\n\n<pre><code>{\n   string v( "bob" );\n   string k;\n\n   v = k\n   // v now contains "bob"\n} // v + k are destroyed here, and any memory used by v + k is freed\n</code></pre>\n'}, {'answer_id': 30238, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 2, 'selected': False, 'text': "<p>In addition to the other answers:</p>\n\n<p>The C++ language actually has the <code>auto</code> keyword to explicitly declare the storage class of an object. Of course, it's completely needless because this is the implied storage class for local variables and cannot be used anywhere. The opposite of <code>auto</code> is <code>static</code> (both locally and globall).</p>\n\n<p>The following two declarations are equivalent:</p>\n\n<pre><code>int main() {\n    int a;\n    auto int b;\n}\n</code></pre>\n\n<p>Because the keyword is utterly useless, it will actually be recycled in the next C++ standard (“C++0x”) and gets a new meaning, namely, it lets the compiler infer the variable type from its initialization (like <code>var</code> in C#):</p>\n\n<pre><code>auto a = std::max(1.0, 4.0); // `a` now has type double.\n</code></pre>\n"}, {'answer_id': 4329919, 'author': 'Francois', 'author_id': 400925, 'author_profile': 'https://Stackoverflow.com/users/400925', 'pm_score': 1, 'selected': False, 'text': "<p>Correct me if i'm wrong, but i think that copy operation is not mandatory to take full advantage of automatic stack cleaning.\nFor example consider a classic MutexGuard object, it doesn't need a copy operation to be useful as stack automatic, or does it ?</p>\n"}]
['https://Stackoverflow.com/questions/30099', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/841/']