Where is the memory allocated for the local C ++ vector?

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

f(){ vector<int> vi; } 
+6
c ++ vector
source share
4 answers

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

+19
source share

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

I asked a question on how to make a stack repository in a vector, and got this answer. You may find it interesting.

+5
source share

To expand on Yacoby's answer, RAII means that when vi goes out of scope, everything that is highlighted with new (inside a vector) delete d (in a vector destructor). This is how you mix stack distribution and heap.

+3
source share

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

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

+3
source share

All Articles