简体   繁体   English

STL C ++中的内存分配

[英]Memory Allocation in STL C++

I am a little confused about memory reallocation in STL C++. 我对STL C ++中的内存重新分配感到有些困惑。 For example, I know if I declare a vector , and keep pushing back elements into it, the vector will at some point need a reallocation of memory space and copy all the existing elements into it. 例如,我知道如果我声明一个vector ,并继续向后推回元素,向量将在某个时刻需要重新分配内存空间并将所有现有元素复制到其中。 For linked lists no reallocation is needed, because the elements are not stored consecutively in the stack and each element uses a pointer to point to the next element. 对于链表,不需要重新分配,因为元素不是连续存储在堆栈中,每个元素都使用指针指向下一个元素。

My question is, what's the situation for other STL in C++ ? 我的问题是,C ++中其他STL的情况如何? for example, string , map , unordered_map ? 例如, stringmapunordered_map Do they need reallocation ? 他们需要重新分配吗?

(disclaimer: all the concrete data structures specified here may not be required by the standard, but they are useful to remember to help link the rules to something concrete) (免责声明:此处指定的所有具体数据结构可能不是标准所要求的,但它们对于记住帮助将规则链接到具体内容非常有用)

std::string ~= std::vector ; std::string ~ = std::vector ; it's a dynamic array, if you keep pushing elements at its end, at some time it will reallocate your elements. 它是一个动态数组,如果你继续推动元素的结束,它会在某个时候重新分配你的元素。

std::list : each element is a new linked list node, so no reallocation ever takes place, wherever you may insert the new element. std::list :每个元素都是一个新的链表节点,因此无论何时插入新元素,都不会发生重新分配。

std::deque : it's typically made of several pages of elements; std::deque :它通常由几页元素组成; when a page is full, a new one is allocated and added to the list of pages; 当页面已满时,将分配一个新页面并将其添加到页面列表中; for this reason, no reallocation of your elements ever takes place, and your elements never get moved if you keep pushing stuff at the beginning or at the end. 因此,不会重新分配您的元素,如果您在开始或结束时继续推送内容,则您的元素永远不会被移动。

std::map / std::multimap / std::set / std::multiset : typically a binary tree, each element is allocated on its own; std::map / std::multimap / std::set / std::multiset :通常是一个二叉树,每个元素都是自己分配的; no reallocations are ever performed. 没有进行任何重新分配。

std::unordered_map / std::unordered_multimap / std::unordered_set / std::unordered_multiset : a hash table; std::unordered_map / std::unordered_multimap / std::unordered_set / std::unordered_multiset :哈希表; when the table is full enough, rehashing and reallocation occurs. 当表格足够时,会发生重新散列和重新分配。

Almost all STL containers memory is allocated on heap, even for a vector. 几乎所有STL容器内存都在堆上分配,即使对于向量也是如此。 A plain array and std::array template are the only once probably that have memory on stack. 普通数组和std :: array模板是唯一可能在堆栈上有内存的模板。

If your questions is about continuous memory allocation (whether on stack or heap), then, probably, plain array, std::array, std::vector have got continuous memory. 如果您的问题是关于连续内存分配(无论是在堆栈还是堆上),则可能是,plain数组,std :: array,std :: vector都有连续内存。 Almost all other containers such as list, deque, map, set etc. do not have memory allocated continuously. 几乎所有其他容器(如list,deque,map,set等)都没有连续分配的内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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