tags:

views:

467

answers:

4

In my browsings amongst the Internet, I came across this post, which includes this

"(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."

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?

Can someone give an example?

+1  A: 

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).

MyObject x = MyObject(params); // onto the stack

MyObject * y = new MyObject(params); // onto the heap

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.

Brad Barker
+1  A: 

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.

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.

Danail Nachev
+11  A: 

Stack objects are handled automatically by the compiler.

When the scope is left, it is deleted.

{
   obj a;
} // a is destroyed here

When you do the same with a 'newed' object you get a memory leak :

{
    obj* b = new obj;
}

b is not destroyed, so we lost the ability to reclaim the memory b owns. And maybe worse, the object cannot clean itself up.

In C the following is common :

{
   FILE* pF = fopen( ... );
   // ... do sth with pF
   fclose( pF );
}

In C++ we write this :

{
   std::fstream f( ... );
   // do sth with f
} // here f gets auto magically destroyed and the destructor frees the file

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).

Another example, demonstrating the object string, which can be constructed, assigned to and which is destroyed on exiting the scope.

{
   string v( "bob" );
   string k;

   v = k
   // v now contains "bob"
} // v + k are destroyed here, and any memory used by v + k is freed
Christopher
This feature is widely used in C++ to automatically manage resources acquisition and release, through the Resource Acquisition Is Initialization (RAII) idiom.
Luc Touraille
for completness: http://en.wikipedia.org/wiki/RAII
Tobias Langner
+1  A: 

In addition to the other answers:

The C++ language actually has the auto 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 auto is static (both locally and globall).

The following two declarations are equivalent:

int main() {
    int a;
    auto int b;
}

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 var in C#):

auto a = std::max(1.0, 4.0); // `a` now has type double.
Konrad Rudolph