Comment by coherentpony
12 years ago
You still have to 'free' in destructors in C++. Destructors don't free your memory for your custom types for you.
12 years ago
You still have to 'free' in destructors in C++. Destructors don't free your memory for your custom types for you.
You need to free heap-allocated objects, but stack allocated objects are important and useful (particularly with RAII) and cleanly reversing such allocations eliminates a class of bugs.
And of course, you don't even need to explicitly free heap-allocated objects if you're using C++11 smart pointers like shared_ptr and unique_ptr.
... mostly. Much like in a garbage collected language, you need to make sure you are no longer referencing things when you want resources to be freed, but it does make the worst case much harder to run into.
Stack allocated objects are cleaned up automatically in C, too...
RAII. [1]
Stack allocated objects have their memory freed in C. Other resources are not released.
Consider the following C, with and without gcc extensions:
with gcc extensions:
without gcc extensions:
[1] http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initial...
The point is that the 'free' is now in one place and not in every place that uses the class.
You mean heap allocated types? Stack allocated will be deallocated implicitly.