简体   繁体   中英

std::vector- how much memory will he allocate (during reallocation)

I know that if vector needs more memory, it allocates new, and somethimes will reallocate his elements. I got a question. Is there any way to check exactly when vector will allocate new memory, and (that's more important) how much will he allocate?

I guess allocation will start exactly when there is no more memory left, and that's why I'm more interested in realloction.

When a new item is added to std::vector<> and it's capacity is equal to it's size then it will allocate new block of memory and move all the items it holds to the newly allocated memory (using Microsoft compiler it is going to allocate 1.5 amount of space of current capacity, if I'm not much mistaken) and add the new one. The previous memory will be deallocated of course

Allocation strategy is implementation-defined . You can track memory allocations with custom allocator or with a memory profiler tool such as valgrind or Visual Studio.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM