-1

I know that if I have created a vector on the stack as follows:

void foo(){
std::vector<int> vec;

}

After foo finishes executing, the memory allocated to vec is released. However, how I do free this memory within foo. I am not looking for vec.clear() as that just clears the container, it does free the memory allocated to the container.

Programmer
  • 6,565
  • 25
  • 78
  • 125

4 Answers4

7

Use a {} scope block to control the vector's lifetime more closely:

void foo()
{
   {
      std::vector<int> vec;
      // stuff that uses vec
   }

   // stuff that doesn't
}

You can use shrink_to_fit in C++11, and attempt trickery-related equivalents in C++03, but why?!

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
2

You need to do absolutely nothing. It will take care of itself, as the destructor in the vector class destroys the vector, and frees the memory when the variable goes out of scope at the end of the function.

Edit: If what you really want to do is something like read in a bazilion numbers, and then remove all the ones that aren't evenly divisible by 6, 7, 2 but aren't equal to 42, then I'd say it's better to create a second vector to store the numbers you wan't to keep, and then let the original vector go out of scope.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • I do not want to wait till the end of the function – Programmer Dec 31 '12 at 17:39
  • 3
    Then scope it more closely! You can't destroy the object until it goes out of scope. You could resize(0), but it's not guaranteed that it it will do anything other than set the size to zero. – Mats Petersson Dec 31 '12 at 17:41
0

If vec.resize(0); vec.shrink_to_fit(); (C++11) as proposed above is not available, you can get something very similar with vector<int>().swap(vec);. Though putting it into an extra {} block is preferrable, if this can be done (not always easy, but usually doable).

Note, however, that swapping with a temporary will quite possibly do a (needless) dynamic allocation for the temporary object that you swap and leave a small block allocated until the end of the current scope.
It will also obviously not free the 4-8 bytes of memory that the actual vector object -- as opposed to the data it holds -- occupies on the stack (only end of scope can do that).

That said, in most cases, the entire endeavour will be kind of futile. If you don't need huge amounts of memory later in the same scope, it doesn't really matter whether you free the vector early or late. Either it already failed much earlier (in that case, the entire problem doesn't arise!), or it will work to the end of the function either way.

On the other hand, if you know that you will need huge amounts of memory later in the same function (say, another vector of similar size) and it is reasonable to expect that this will become a problem, it is hard to predict whether freeing the first really makes a difference (think fragmentation, especially when doing the swap trick).
You can only expect this to work reliably if the second vector is the same size or smaller than the first one, in every other case it's not guaranteed to work any better.

Damon
  • 67,688
  • 20
  • 135
  • 185
0

In my opinion, you could release full memory which was allocated in vector by the way:
1. Using method clear() to resize the vector, and then using shrink_to_fit() to make sure that all elements in vector are actually released.
2. In case your vector contained a pointer element, using smart pointer (shared_ptr, unique_ptr) instead of raw pointer (in ANSI C).
3. If you used raw pointer, donot forget to free() them when stopping using them any more.

nvnhcmus
  • 1
  • 3