Since container data types have dynamic size I'm assuming they allocate memory on the heap. But when/how do they free this allocated memory?
-
Actually, containers can be allocated on either the heap or the stack: http://stackoverflow.com/questions/2388224/where-is-the-memory-for-a-local-c-vector-allocated – Himadri Choudhury May 23 '11 at 02:29
-
3@DasBoot: The container itself can be allocated in either place. However, the contents of that container are typically heap allocated. (Modulo small string optimization cases) – Billy ONeal May 23 '11 at 02:33
3 Answers
They get freed either when they go out of scope (if the container was created in the stack), or when you explicitly call delete on the container(in case of heap-based container). When this happens, the destructor of the container automatically gets called and the heap memory allocated for the container (that contains the data) are freed then.
Simply removing an element in the container won't necessarily free the memory right away, since STL containers generally use caching to speed things up. Remember, new/delete operations are relatively costly.

- 8,465
- 4
- 34
- 62
-
1This is true for vector (in terms of keeping the memory around), but it's typically not true for any of the other containers. (That is, only `vector` and `string` will keep the memory around -- deque, list, set, and map will all *usually* deallocate) – Billy ONeal May 23 '11 at 02:42
-
1@BillyONeal: Deque typically won't deallocate when removing a single item. – Thomas Edleson May 23 '11 at 03:10
-
@Thomas: Yes, that's true. However the amount of extra space usually is never larger than the size of a "bucket" – Billy ONeal May 23 '11 at 03:33
-
Nobody loves them, everybody hates them, they're going to the garden to eat worms... `static` containers not worth a mention...? ;-) – Tony Delroy May 23 '11 at 04:24
-
@Tony: I would consider statics just like automatic variables whose scope is the whole program. However you do bring up an interesting point. :) Nobody thinks too much about how statics work because usually the amount of memory they occupy is small in real world programs. – Billy ONeal May 23 '11 at 04:51
-
1@Billy: poor static locals - still ignored ;-). There are still plenty of programs around that use them for singletons (esp. on compilers where it's implicitly thread-safe to do so e.g. GCC sans `-fno-threadsafe-statics`), and the memory use of singletons is often non-trivial: in some programs they dominate as they're used akin to an internal database. Unfortunately, I deal with this daily :./ – Tony Delroy May 23 '11 at 06:00
They free the memory in their destructors when they are destroyed. (And they are destroyed by having delete
or delete []
called if the container itself is heap allocated, or by going out of scope if it is stack allocated)

- 104,103
- 58
- 317
- 552
-
how does the destructor get automatically called when execution goes out of scope? – Jeff Chen May 23 '11 at 02:38
-
3@Jeff Chen: It's a language feature, that destructors are called automatically when the lifetime of a variable ends. – Puppy May 23 '11 at 02:40
-
@Jeff: The same way it gets called automatically for any other stack allocated (also known as "automatic") variable. The language guarantees that as soon as an object goes out of scope, its destructor will be called. – Billy ONeal May 23 '11 at 02:40
Short answer: When you remove elements from it.
Generally it happens when you remove element from the container blow its previous growth threshold. It's an implementation detail, but usually for example a vector creates an internal array of N T's.
When you insert more than N of T's, then the vector realocates it memory and grows to some multiple of N (again - implementation detail) to store the new elements, same happens with removal - when your remove elements from your vector it shrinks whenever it reaches the previous multiple of N... until you end up with only one multiple of N, or 0 if you clear it and shrink it with
The heap memory (node storage) is deleted also when the vector object is destructed.

- 3,606
- 4
- 32
- 48
-
Note: Most vector implementations don't actually reallocate when they shrink -- they simply keep the buffer around. – Billy ONeal May 23 '11 at 02:39
-
To add to @Billy's point, there's a swapping trick you can use to shrink the storage: `vector
(vec.begin(), vec.end()).swap(vec)`. – C. K. Young May 23 '11 at 02:48 -
[Herb Sutter](http://stackoverflow.com/users/297582/herb-sutter) says the "shrink to fit" trick works for `deque` as well. `deque` also appears to hang on to its memory. http://www.gotw.ca/gotw/054.htm – Fred Larson May 23 '11 at 02:58
-
@ChrisJester-Young: And in C++0x, we spell that as vec.shrink_to_fit(). – Fred Nurk May 23 '11 at 03:14