Effective STL Terms 13

zhaozj2021-02-11  178

Terms 13: Try to use Vector and String to replace the array of dynamic allocation

At this moment, you decided to use New to make dynamic allocation, you need to shoulder the following responsibilities:

You must ensure that some people will distribute this later. If there is no delete behind, your NEW will generate a resource leak. You must ensure the correct form of Delete. For assigning a separate object, you must use "delete". For assigning an array, you must use "Delete []". If you use Delete's error form, the result is not defined. On some platforms, the program will be changing in the runtime. On the other hand, it will silently go to the error, sometimes it will cause resource leaks, and some memory will come. You must make sure only delete is once. If an allocation is removed more than once, the result is not defined again.

Duty is really much, and I can't understand why you can worry that you have to be responsible. Thanks to Vector and String, you can use them you can not be as troubled before.

Whenever, you find that you are ready to dynamically assign an array (that is, trying to write "New t [..."), you should first consider using a vector or a string. (Generally, when t is a character type, use String, otherwise use vector, but behind this Territor, we will encounter a vector may be a reasonable design choice.) Vector and String Eliminate the burden on it because they manage their memory. When the elements are added to those containers, their memory grows, and when a vector or string destroys, its destructive function automatically destroys the elements in the container and reclaims the memory of those elements.

In addition, Vector and String are a febrile sequence container that allows you to give you the entire STL algorithm of such containers. Although the array can also be used for STL algorithms, it is not provided with member functions like Begin, End and Size, and is not in TypeDef like Iterator, Reverse_Iiterator or Value_Type. And the char * pointer certainly cannot and provides a String competition for the dedicated member function. The more STL is used, the more it will discriminate against built-in arrays.

If you care about the legacy code you must continue, they are all based on array, relax, and whenever you should use Vector and String. Terms 16 demonstrates how easy it is to transmit data from VECTOR and String to the API that needs Array, so the legacy code is generally no problem.

Frankly, I thought of a (and only one) with a Vector or String instead of the dynamic allocation array, and it only relates to String. Many String implementations use the reference count (see Terms 15) in the background, and an unnecessary memory allocation and character copying strategy, and performance can be improved in many applications. In fact, it is generally considered to be important by reference to the counting optimization string, so the C Standards Committee specially managed that it is a legal implementation.

Hey, an optimization of a programmer is the complaints of others, and if you use a string of reference counts in a multi-threaded environment, you may find that the time that avoids allocation and copies, it costs in the background concurrency control. (For details, please refer to the Sutter article "Optimizations That Aren't" [20].) If you use a reference count string in a multi-threaded environment, you should pay attention to the thread security support. Performance decline.

To know if the String implementation you are using is a reference count, the simplest way is the documentation of the reference library. Because the reference count is usually considered to be an optimization, making makers typically be touted as a characteristic. Another method is the source code of the String implementation of the library. I generally don't recommend trying something from the library source code, but sometimes this is the only way you want to find what you want to know. If you choose this method, you have to remember that String is a Basic_String typefef (WString is a typefef of Basic_String , so you really need to see the Basic_String Template. The easiest way to check is a possible class constructor. See if it adds a reference count in somewhere. If so, string is a reference count. If not, either String is not a reference count, or you have a wrong code. Ha ha. If the String implementation you use is a reference count, you want to run in a multi-threaded environment that has already determined String is a multi-threaded environment that has a performance problem, and you have at least three reasonable options, and there is no given STL. First, see if your library implements whether you can close the reference count, usually by changing the value of the preprocessing variable. Of course, it is not portable, but makes the work possible, worth studying. Second, find or develop a String implementation (or partially implemented) alternative to not using the reference count. Third, consider using Vector instead of String, the Vector implementation does not allow the reference count, so the hidden multi-thread performance issues will not appear. Of course, if you have selected Vector , you give up the String's dedicated member function, but most of the function can still be obtained through the STL algorithm, so you switch from a syntax to another will not lose a lot of features.

All results are simple. If you are using dynamic allocation arrays, you may do more than you need. To alleviate your burden, use the Vector or String instead.

转载请注明原文地址:https://www.9cbs.com/read-4802.html

New Post(0)