Sunday 3 February 2013


RAII (Resource Acquisition Is Initialization):

C++ doesn't have (built-in) garbage collection,but that doesn't mean you should let your dynamically allocated memory stay allocated forever. You can and should free it. The most basic way to that would be to free your object reference (or pointer, since this is C++)manually when you don't need it anymore:

for ( int i = 0 ; i < 100 ; i ++) { 
// Dynamically allocate an object 
Object * o = new Object (); 
// do something with object 
// Release objectmemory (and call object destructor, if there isone) 
delete o ; 
}

If you allocated your object on the stack however, it always gets automatically released when it goes out of scope, and you don't have to wait for garbage collection to happen
- it's always released immediately:

for ( int i = 0 ; i < 100 ; i ++) { 
// Create a newobject on the stack 
Object o = Object (); 
// Note we're not using the new keyword here. 
// Do something with object 
// Object gets automatically deallocated, or more accurately, in this specific
// case (a loop), the compiler will optimize things, so object's destructor
// will get called, but the object's stack memory will be reused. 
}

This behavior of C++ stack values (automatic destruction when they go out of scope), which is awkwardly termed
RAII (Resource Acquisition Is Initialization) allows for very nice things that Java just can't do. One of them is smart pointers, that allow dynamicallyallocated objects to be freed automatically just like their stack counterparts, and you canuse sophisticated smart pointers to implement your own version of garbage collection if you want.
Another advantage of RAII is that finally-blocks are rarely necessary in C++: local variables that reference to a resource that should be freed immediately are usually allocated on the stack, and therefore get released automatically. No need for finally blocks here.

Practically speaking, when programming in C++ you'd usually put local variables on the stack (and get all the advantages of RAII without lifting a finger), unless you need to keep them alive for longer than that (e.g. you need to create an object and store a reference to it that stays alive when you leavethe function that created the object). In that case, you can use pointers directly, but if you don't want to deal with manually deleting pointers and all the problems it can lead to, you'd usually use a smart pointer. A smart pointer is an object allocated on the stack, that wraps a 'dumb' (i.e. regular) pointer, anddeletes the pointer when its destructor gets called. More advanced version of smart pointers can implement reference counting (so the pointed object will be released only when all smart pointers referencing it go out of scope) and even garbage collection. The standard library comes with two smart pointers: auto_ptr and smart_ptr (the latter is reference-counting, but some old compiler may not support it).



 I don't have the exact quotes but both Bjarneand Herb Sutter says something along the lines:
"C++ doesn't need a garbage collector, because it has no garbage."

In modern C++ you use smart pointers and therefore have no garbage.

The C++ approach to memory management is newer than garbage collectors. RAII was invented by Bjarne Stroustrup for C++. Destructor cleanup is an older idea, but the rules for ensuring exception-safety are key. I don't know when exactly when the idea itself was first described but the first C++ standard was finalized in 1998, and Stroustrups "Design and Evolution of C++" wasn't published until 1994, and exceptions were a relatively recentaddition to C++ - after the publication of the"Annotated C++ Reference Manual" in 1990, I believe. GC was invented in 1959 for Lisp.

No comments:

Post a Comment