Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is std::string/std::map not encouraged to be created on the heap?

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?

like image 253
dev_nut Avatar asked May 29 '13 05:05

dev_nut


People also ask

Is std :: string allocated on the heap?

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.

Is std :: string allocated on stack or heap?

So we can say std::string allocates short strings on the stack, but long ones -- on the heap.

Is map stored in heap?

At any rate, map is usually implemented as a red-black tree and tree nodes are on the heap.

What is the complexity of std::map :: insert () method?

Time complexity: k*log(n) where n is size of map, k is no. of elements inserted.

What is std::map used for?

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.

Is std::map ordered?

Yes, a std::map<K,V> is ordered based on the key, K , using std::less<K> to compare objects, by default.


1 Answers

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.

like image 64
Jerry Coffin Avatar answered Oct 05 '22 23:10

Jerry Coffin