C ++ standard STL-sgi stl source code learning notes (08) String

Source: Internet
Author: User
Tags string methods try catch

From the holiday to the present, I haven't touched STL source code for a long time. During the time I went home, I learned Lua. Lua had less learning materials and sent some time.

About Lua'sArticleYou can only say that you have time to talk about it. If you are okay, you can play with QT. The article will be added later.

 

I slept for a whole day yesterday and went to the network center in the evening. I sat down in the familiar environment, but I still couldn't escape from my heart.

That feeling. However, the past should have passed, and time can change all of this. This is just a matter of time.

 

For more information about STL, I recommend www.cplusplus.com. Sometimes I also like to use the documents on this website as a reference, and then

Combined with the sgi stl source code, we can have a good understanding and in-depth understanding. Well, I will not talk much about it. Let's take a look at the string source code in SGI STL.

 

If you only see the source code of the string, I laugh. Bro, you can never find the source code of the string. Because sgi stl is fond of hiding from the cat, I believe the previous

In a series of articles, we can see this problem. In fact, the basic_string in string is the implementation of string, or in a more accurate sentence,

In fact, string is an implementation of the basic_string template class. You can see in the stl_string_fwd.h file:

Now that we know that basic_string is the native Implementation of string, it is useless to say anything else.CodeRight.

 

As an example, the three variables do not need to be explained much. The underlying implementation of STL is like this.

 

Sgi stl does a lot of work in memory management, so the string source code also needs to have. Like _ string_alloc_base. I have read the previous analysis article,

There is no good way, So let's think about it.

 

As for _ string_base, it is actually to share part of the basic content in basic_string. If it is written in basic_string, there is no problem.

However, there are several methods that need attention. This is related to the implementation of commonly used string methods. It is also related to the previous memory management.

.

Note: _ m_allocate returns the requested memory header pointer.

_ M_allocate_block calls the _ m_allocate method, but checks the parameters and initializes the basic variables. In other words, it is _ m_allocate.

Method completion.

_ M_deallocate: The applied memory is released. If the parameter n is correct, it should be ineffective. I have analyzed the memory in the previous article.

Release source code, which has been mentioned there.

_ M_deallocate_block is the same as above. It is perfect for the _ m_deallocate_block method.

 

In fact, the improvement of the _ m_allocate_block and _ m_deallocate_block methods is not unreasonable. From the _ string_base constructor

To see.

(I believe this is the same as the previous one. I don't remember it ~~ )

There are two methods, both of which are incorrect.

 

Basic_string private inheritance _ string_base can also be seen because the _ string method is related to memory application release and data initialization.

So it should not be exposed.

 

Let's take a look at the constructors of the basic_string template class.

This constructor should take a closer look. non-parametric constructor prohibits the implicit automatic conversion of evil compilers. We can see that the default initial data

The storage space is 8 bytes. (Note that the storage space is not the size of basic_string, but the size of the memory space pointed to by _ m_start .)

_ Stl_try & _ stl_unwind is the function of try catch.

Because there are many source code levels, the call between them can also be found through the jump, so I only provide the final source code used by construct, please refer to the following.

Place an element on the _ m_finish position using placement new. I have been thinking for a long time, but I didn't want to understand it. If you want to know it, please let me know.

In the end of this article, destory. Both _ m_terminate_string and _ m_construct_null appear in the front, but the parameters are different.

Okay, this is actually described earlier.

Cyclically release space between _ m_start-> _ m_finish.

In fact, the next step is to call the previous _ destory function. We will repeat it here.

 

This article has been updated for a long time and is busy. However, after the start of the school year, there will be a lot of leisure in the future.

I am looking for a candidate to analyze the source code of an open-source project or library with me. It takes some time to find the source code. Then you may consider doing it yourself.

 

In the next article, I will introduce the remaining methods and other source code in the string source code. If there is any error, please make a brick. Thank you.

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.