Free memory occupied by Std List, Vector, Map, etc.

Based on the background of C #, I only have a vague idea of ​​managing memory in C ++ - all I know is that I will have to free memory manually. As a result, my C ++ code is written in such a way that objects like std::vector , std::list , std::map freely created, used, but not freed.

I did not understand this moment until I finished my programs, now my code consists of the following types of templates:

 struct Point_2 { double x; double y; }; struct Point_3 { double x; double y; double z; }; list<list<Point_2>> Computation::ComputationJob (list<Point_3>pts3D, vector<Point_2>vectors) { map<Point_2, double> pt2DMap=ConstructPointMap(pts3D); vector<Point_2> vectorList = ConstructVectors(vectors); list<list<Point_2>> faceList2D=ConstructPoints(vectorList , pt2DMap); return faceList2D; } 

My question is: should I free every.single.one from using the list (in the example above, this means that I will need to free pt2DMap , vectorList and faceList2D )? That would be very tiring! I could just rewrite my Computation class so that it is less prone to memory leak.

Any idea how to fix this?

+4
source share
3 answers

No: if objects are not allocated using new , they should not be explicitly freed / deleted. When they go out of scope, they are automatically released. When this happens, the destructor is called, which should free all the objects to which they relate. (This is called Resource Initialization, or RAII , and standard classes such as std::list and std::vector follow this pattern.)

If you are using new , you should either use the smart pointer ( scoped_ptr ) or explicitly call delete . The best place to call delete is in the destructor (for security reasons exceptions), although smart pointers should be preferred when possible.

+10
source

In general, I can say that standard C ++ containers make copies of your object under the scene. You have no control over this. What does this mean that if the construction of your objects ( Point_2 in your case) includes any allocation of resources (for example: new or malloc calls), then you will have to write your own versions of copy constructors and destructors that make this behave reasonably, when your map decides to copy Point_2 around. This usually includes methods such as link counting.

It’s much easier for many people to simply put pointers to a complex object in standard containers, and not to the objects themselves.

If you don’t do anything special in the constructor or destructors for your objects (which at present seems to be for you), then there is no problem at all. Some containers (for example, maps) will make dynamic selections under the scenes, but this is virtually invisible to you. Containers worry about resource allocation. You only need to worry about yourself.

+2
source

All stl containers automatically clear their contents, all you need is to clear the data that you allocate dynamically (i.e. rule: take care of pointers).

For example, if you have a list<MyType> - the list contains objects of some custom type inside - when destroyed, it is called ~MyType() , which should take care of properly clearing the contents of the object (i.e., if MyType has some pointers to allocated memory inside, you have to remove them in the destructor).

On the other hand, if you start using list<MyType*> - the container now does the cleaning, it contains some scalar values ​​(like integers) and only deletes the pointers without clearing the pointed content, so you need to clear it manually.

Really good advice (helped me many years ago :)) when switching from Java / C # to C ++ - carefully monitor the life cycle of a dynamic memory object: a) where it is created, b) where it is used, c) where and when he is removed. Make sure that it is cleaned only once and after that does not refer!

0
source

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


All Articles