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

I noticed a few questions, C ++ experts are asking what std :: string / std :: map / etc. should not be created using a new key (new to C ++, if this was not obvious).

So, if my understanding is correct, this will not create it on the heap, but on the stack. This would mean that at the moment when the function goes out of scope, the object will disappear, but I believe that this is not so, and my understanding is incorrect.

Is it because the base template creates an instance on the heap and manages it with auto_ptr so that it does not cause a memory leak? Does this apply to all stl classes?

In addition, the next question is what should be the approach to creating objects inserted in maps? Should they be heaped (if they are valuable outside the scope of the function)?

EDIT:

I understand the difference between the heap and the stack and the reasons for using each (I probably didn't quite understand this).

The reason I ask for this seems unnatural, just to create an instance of the object in the stack for the object that I would like to save. But, I think that is what the syntax looks like.

This means that I feel that I have 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 am also interested in learning about the effect of this on memory. Since the memory is now cleared by the implementation itself, is garbage collection similar in Java? Is there an implied performance impact when using the stl object?

+3
source share
2 answers

One common reason to put things in a heap is that the size is not known at compile time, and you need the flexibility to allocate some unknown number of objects. Internal implementations of string and map and other containers will automatically be allocated from the heap as necessary, freeing you from this load. Putting an object in a heap becomes redundant.

The only reason to put a container in a heap is when you need its life to be longer than the block that creates it.

+5
source

This does not apply to std::string or std::map . This is just a general rule for almost all objects in C ++.

To automate resource management, you usually want to associate each resource with a certain area that spans the time that the resource will be required. Assuming you do this sequentially, each resource is cleared automatically when execution leaves the area in which it was defined.

This is most often called "RAII" (Resource Initialization), although some use the more descriptive term SBRM (Scope Tied Resource Management). Regardless of the terminology you use, with appropriate (and consistent) use, it can work very well.

The reason for the term "RAII" is that it means that most of the resources are acquired when the object is built and freed up when the object is destroyed. This, as a rule, leads to a rather definite coding style, where (among other things) objects are rather detailed, since each controls the lifetime of one particular resource.

+14
source

Source: https://habr.com/ru/post/1483283/


All Articles