简体   繁体   中英

Memory allocation for C++ STL objects

Suppose I am creating an object of STL map in C++ ie map<string,char>mymap``$ . Now, I want to know how memory is being allocated to this object? My point of confusion is that there is no dynamic allocation and we don't know the size of the container in prior. So, from where and how memory is allocated to the object mymap ?

there is no dynamic allocation

Sure there is, but it's under the hood. std::map is usually stored as a tree, so a new node is allocated whenever a new object is inserted into it. Dynamically. Just because you don't explicitly write new , it doesn't mean it doesn't happen under the hood.

On the destructor of std::map , the nodes are automatically deleted. Note however that if the nodes contain dynamically allocated objects, those will not be deleted by the map.

So, from where and how memory is allocated to the object mymap?

The default allocator for all Standard containters is std::allocator , from where your container gets memory and releases to when it is done with the memory. You can use custom allocator, and then keep track of all the allocations and deallocations if you want to.

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