If std::vector and friends are self resizing, does that mean if I declare a vector like so:
std::vector<string> myvec;
Then it'll resize using more stack, whereas:
std::vector<string> *myvec = new std::vector<string>();
Would resize using more heap?
Containers. The STL contains sequence containers and associative containers. The containers are objects that store data. The standard sequence containers include vector , deque , and list . The standard associative containers are set , multiset , map , multimap , hash_set , hash_map , hash_multiset and hash_multimap .
Types of STL Container in C++ In C++, there are generally 3 kinds of STL containers: Sequential Containers. Associative Containers. Unordered Associative Containers.
Vectors allocate on the heap in their internals.
The only thing you pay for in the stack for a stack based bector is a couple of bytes, the inner buffer will always be allocated from the heap.
So effectively when you do a vec = new vector() you are allocating a small quantity, which may not be really good.
In the first case, you are creating the vector on stack. That doesn't mean that all the vectors internal objects are on stack as well. In fact, vector will still allocate the memory required to hold the objects on heap only. This is because, to allocate on stack you should know how many objects to create. But this information is not available, so the only remaining option is to allocate the memory for the contained object from heap.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With