Pointers in containers. STL (part 16)

Indices in containers, STL, Programming for beginners

STL containers significantly reduce the complexity of writing code, working with dynamic data structures and, the most important thing, increase its inerrancy. But in some cases, when working with large data items in containers, This technique can produce the inevitable loss of productivity. This is because, that since the operation room a container element, and subsequent displacement elements may require a copy of elements.

Note: The severity of these effects depends on the type of container, and from the transactions. For example, interchange operation 2 elements in the sorting require 3 copyings type container vector and will not require additional copy for container list. But the operation of the initial space in the container (push_back(), insert() and etc.) always make a copy.

This can be a problem for an application, when operations on the containers is critical for runtime (or seem to you such). There are even more complicated cases, when a class container object is not picked up operation, or when the objects are referenced objects themselves, full backups for which you want to perform recursive procedures following all the links (then, in Python, and the other is called a deep copy).

Everything works well, as before - sort a set of records on arbitrary fields and in any order:


(Note, that completing this application you need for Ctrl + D - End Of File ... in the Widows, probably, по Ctrl + Z.)

But! ... Especially debugging "traces" positives were left constructors and destructors records, and recording constructed 8 times, and the destructor is only triggered 4, for local array at the exit point of the block. Local array generally not of interest to us. He was introduced to simplify the example, only as a set of initialization values. But for the record, placed in a container, records destruction does not occur, and we get a candid memory leak. But worse, after we remove the item from the container (without taking further action), we can not delete the record, calling for it delete. This is because, after call erase() We lost to a record only path through the iterator (the code shows a loop erase(), so graphically, that is equivalent to clear(), the effect of which, It is the same).

Conclusion, which can be made from Example, looks like that:

  • placing objects in containers not, and Indices on them, can significantly reduce the computational cost of the manipulation (but always principled whether these gains at the current computing power?).

  • replacement facilities pointers in containers makes the code is much more dangerous in the sense of hidden subtle bugs, And such a serious level, which can further lead to an application crash.

Here some help may have smart pointers of the latest C ++ standard (shared_ptr or weak_ptr, but not unique_ptr and not the good old and problematic auto_ptr), us for this in the previous code, just change 4 strings:

In Windows, shared_ptr needed #include <memory> , and in other systems, not necessarily.

And application behavior change significantly:


But we should not delude ourselves recklessly, so as smart pointers, removing some, generate other potential concerns (such as cyclic links etc.. about which much is written enough).

Newsletter of programming:

Pointers in containers. STL (part 16)
3 (60%) 4 votes

About Olej

practical experience about software development 40 years. Teacher Global Logic international software company. IBM Developer Works Permanent author of publications. Scientific editor of the computer literature publishing house "Symbol-Plus", St. Petersburg.

Leave a Reply

Your email address will not be published. Required fields are marked *