Can I use blocks to manage memory consumption in C++?

c++, object, scoping

Solution

Yes, you can.

The destructor will be called as soon as the variable falls out of scope and it should release the heap-allocated memory.

Problem

I'm trying to gain some memory saving in a C++ program and I want to know if I can use blocks as a scope for variables (as in Perl). Let's say I have a huge object that performs some computations and gives a result, does it makes sense to do: ``` InputType input; ResultType result; { // Block of code MyHugeObject mho; result = mho.superHeavyProcessing(); } /* My other code ... */ ``` Can I expect the object to be destroyed when exiting the block?

Original source