I have noticed on multiple questions, C++ experts asking that std::string/std::map/etc. should not be created with the 'new' keyword (newbie to C++, if it wasn't obvious).
So, if my understanding is correct this would not create it on the heap but on the stack. This would mean that the moment the function goes out of scope, the object would disappear, but I believe that is not the case and my understanding is wrong.
Is this because the underlying template instantiates it on the heap and manages it using an auto_ptr, so that it doesn't cause a memory leak? Does this apply for all stl classes?
Also, a follow up question is what should be the approach to create objects that are inserted in maps? Should they be allocated on the heap (if they are valuable out of the scope of the function)?
EDIT:
I do understand the difference between heap and stack and the reasons for using each (I probably was not clear about this).
The reason I ask for this is it seems unnatural, to just instantiate an object on the stack for an object I'd like to keep around. But, I guess this is just how the syntax looks like.
This means, I feel like I'm having something on the stack when I write,
std::map<int,int> mymap;
instead of,
std::map<int,int> *mymap = new std::map<int,int>;
I'm also wondering about the impact of this on the memory. Since now the memory is cleaned up by this implementation itself, is it analogous to garbage collection in Java? Is there an implied performance impact when using an stl object?
The string object itself is stored on the stack but it points to memory that is on the heap. Why? The language is defined such that the string object is stored on the stack. string's implementation to construct an object uses memory on the heap.
So we can say std::string allocates short strings on the stack, but long ones -- on the heap.
At any rate, map is usually implemented as a red-black tree and tree nodes are on the heap.
Time complexity: k*log(n) where n is size of map, k is no. of elements inserted.
std::map is a sorted associative container that contains key-value pairs with unique keys. Keys are sorted by using the comparison function Compare . Search, removal, and insertion operations have logarithmic complexity. Maps are usually implemented as red-black trees.
Yes, a std::map<K,V> is ordered based on the key, K , using std::less<K> to compare objects, by default.
This isn't particular to std::string
or std::map
. It's simply a general rule about almost all objects in C++.
To automate resource management, you usually want to tie each resource to some scope that encompasses the time that resource will be needed. Assuming you do that consistently, each resource gets cleaned up automatically when execution leaves the scope in which it was defined.
This is most commonly referred to as "RAII" (Resource Acquisition is Initialization), though some use the more descriptive term SBRM (Scope bound resource management). Regardless of the terminology you use, with appropriate (and consistent) use, it can work extremely well.
The reason for the "RAII" term is that this means most resources are acquired during object construction, and released during object destruction. This tends to lead to a fairly specific style of coding, where (among other things) objects are quite granular, as each manages the lifetime of one specific resource.
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