Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

vector

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;
}
like image 730
skydoor Avatar asked Mar 05 '10 16:03

skydoor


2 Answers

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

like image 165
Yacoby Avatar answered Nov 07 '22 21:11

Yacoby


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.

like image 22
Zan Lynx Avatar answered Nov 07 '22 21:11

Zan Lynx