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
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
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.