Valid STL-use vector and string instead of the dynamically allocated array (char *)

Source: Internet
Author: User

 

Use Vector and string instead of the dynamically allocated array.

At this moment, you decide to use new for Dynamic Allocation. You need to shoulder the following responsibilities:

  1. Make sure that some users Delete the allocated memory in the future. If there is no Delete next to it, your new will generate a memory leak.
  2. Make sure that the delete format is correct. To assign a separate object, you must use "delete ". To assign an array, you must use "Delete []". If the delete error format is used, the result is undefined. On Some platforms, the program crashes during runtime. On the other hand, it will silently go wrong and sometimes cause memory leakage.
  3. You must make sure that only delete is performed once. If an assignment is deleted more than once, the result is also undefined.

There are many responsibilities, and I cannot understand why you have to take responsibility if you can worry about it. Thanks to vector and string, using them is not as troublesome as before.

 

Whenever you find that you are preparing to dynamically allocate an array (that is, attempting to write "New T [...]"), you should first consider using a vector or a string. (Generally, string is used when T is a character type. Otherwise, vector is used. However, we will encounter this situation after these terms, vector <char> may be a reasonable design choice .) Vector and string eliminate your burden because they manage their own memory. When elements are added to those containers, their memory will increase, and when a vector or string is destroyed, its destructor will automatically destroy the elements in the container, reclaim memory for those elements.

 

In addition, vector and string are a series container with rich functions. They not only support array operations, but also provide more functions. Although Arrays can also be used in STL algorithms, they do not provide member functions such as begin, end, and size, nor embed typedef like iterator, reverse_iterator, or value_type. And char * pointers certainly cannot compete with strings that provide dedicated member functions.

 

If you are concerned about the legacy code that you must continue to support, they are all array-based and relaxed. Vector and string should be used in any case. Vector and string can be

Native array for seamless connection.

 

Frankly speaking, I think of a (and only one) problem that will occur when we use vector or string instead of the dynamic array allocation, and it is only related to string. Many string implementations use reference counting in the background (see cla15), a policy that eliminates unnecessary memory allocation and character copy, and can improve performance in many applications. In fact, it is generally considered that it is very important to optimize the string by referencing the count, so the C ++ Standards Committee has specifically managed to ensure that it is a legal implementation.

 

Alas, optimization by a programmer is complained by others. If you use a reference string in a multi-threaded environment, you may find that the time saved by avoiding allocation and copying is spent on the background concurrency control. (For details, refer to the Sutter's article optimizations that aren't (in a multithreaded world) [20].) If you use a reference counting string in a multi-threaded environment, pay attention to the performance degradation caused by thread security support.

 

You need to know whether the string implementation you are using is reference counting. The simplest way is to refer to the library documentation. Because it is generally considered that the reference count is an optimization, the producer generally uses it as a feature to boast. Another method is to view the source code of the string implementation of the database. I generally do not recommend you try to get something from the library source code, but sometimes this is the only way to find out what you want to know. If you select this method, remember that string is a basic_string <char> typedef (and wstring is a typedef of basic_string <wchar_t>), so you really need to see the basic_string template.

 

The easiest thing to check is the possible class constructor. Check whether it adds reference count somewhere. If yes, string indicates the reference count. If not, either string is not the reference count, or you have read the wrong code. Haha.

 

If the string implementation you use is reference counting, and you want to run it in a multi-threaded environment that has determined that string reference counting is a performance problem, you have at least three reasonable options, and none of them give up STL. First, check whether your library implementation can disable the reference count, usually by changing the value of the pre-processing variable. Of course it cannot be transplanted, but it makes work possible and worth studying. Second, find or develop a replacement for string implementation (or partial implementation) without reference counting. Third, we should consider using vector <char> instead of string. reference counting is not allowed for vector implementation, so hidden multi-thread performance problems will not occur. Of course, if you select vector <char>, you will discard the dedicated member function of string, but most of the functions can still be obtained through the STL algorithm, so you can switch from one syntax to another without losing a lot of functionality.

 

All results are simple. If you are using dynamic array allocation, you may do more work than you need. To reduce your workload, use vector or string instead.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.