Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ STL question: allocators

Tags:

c++

stl

I have a (potentially dumb) question about the C++ STL. When I make a container (vector, set, map, etc), is it allocated on the stack or on the heap? If I make a set and put 5 million strings, will I have to worry about a stack overflow?

like image 566
Jason Baker Avatar asked Aug 28 '08 20:08

Jason Baker


2 Answers

STL classes by default allocate their internal buffers from the heap, although these classes also allow custom allocators that allow a user to specify an alternate location to allocate from - e.g. a shared memory pool.

like image 57
Brian Stewart Avatar answered Oct 13 '22 02:10

Brian Stewart


The default allocator for STL containers uses operator new and delete, so it's whatever those route to for the type being contained. (In general, it comes from the heap unless you do something to override that.)

You will not get a stack overflow from allocating 5 million strings. Even if you made a stack based allocator, it would probably overflow before you even inserted one string.

like image 27
Mat Noguchi Avatar answered Oct 13 '22 02:10

Mat Noguchi