tags:

views:

153

answers:

4

I noticed that the memory for vector is allocated dynamically. So for a local vector, where does the memory is allocated?

f(){

 vector<int> vi;
}
+16  A: 

The vector is allocated on the stack (28 bytes on my system). The vector contents are allocated on the heap.

Yacoby
+3  A: 

To expand on Yacoby's answer, RAII means that when vi goes out of scope, anything allocated with new (inside the vector) is deleted (in the vector's destructor). That's how you mix stack and heap allocation.

Skilldrick
+5  A: 

You can change how memory is allocated for STL containers with the combination of Allocator template type and the allocator object passed to the constructor.

I asked a question about how to make a vector use stack storage and got this answer. You might find it interesting.

Zan Lynx
+1 Very interesting...
Skilldrick
Definite +1 from me.
Yacoby
+3  A: 

The vector is allocated wherever the allocator it uses decides to allocate from.

In the default case of std::allocator, it uses ::operator new().

MSN