Boost Library is working (2) one of the smart pointers to scoped_ptr

Source: Internet
Author: User

A long time ago, Dennis Ritchie invented the C language, which has the advantage of dynamically allocating memory. When you need to use the memory, you can apply to the system immediately. When you do not need it, you can immediately release it back to the system and use it for other programs. This kind of dynamic memory management allowed the emergence of software that could handle a large amount of data in the memory age of K computing. However, the malloc function is used to allocate memory and the free function is used to release the memory until today. However, one disadvantage of this method is that when you constantly use the malloc function to allocate memory and forget to use the free function to release the memory, the memory in the system will be reduced by 1.1 points. Recall that such a strange thing happened when I didn't have enough understanding of this point when I first started software development. At that time, my boss told me to develop a Stock Information upload software, which has the following features: 24 hours a day, continuous operation for one year, not closed. When the software was launched, it was found that the previous week was running normally. After a week, it was found that the system was extremely slow and the memory was occupied. No memory was available. Then, you can use the memory check tool to view and upload the stock information software, which actually occupies most of the memory. In this case, the memory leakage was discovered. The code search was immediately started, and it took a whole week to complete the solution. It is found that the memory is allocated in some places. Under certain conditions, this part of memory will not be released. After this experience, we will immediately write the code to release the memory after allocating the Memory code. Therefore, memory management is the top priority in programming thinking. Otherwise, the developed software may die again that day and complain to the customer. Later in the C ++ Development era, memory allocation was changed to new and delete operations, and constructor and destructor were also provided to further automate memory management, this is much more convenient than the C language era. However, in C ++, there are still a lot of resources allocated in functions, which are directly returned in some places. If you forget to delete the memory, there is a memory leak. Example: www.2cto.com [cpp] voidTest (void) {int * nVal = new int; if (* nVal> 0) {return;} delete nVal, just a few lines of code, of course, will not forget, but the code for writing a function usually has more than 200 lines. Some people write the code for a function to 1000 lines or more than 2000 lines, and there are also many people. In this case, the memory is often forgotten to be deleted, resulting in leakage. Faced with the high risk of memory loss, the software is unstable for a long time and hard to eradicate. Today, the amount of software code is growing, in the world of C ++, we urgently need to find a solution to solve this problem. Otherwise, the software development costs will increase, and the software release date will be pushed again. Some developers can't wait for this solution to appear, so they turn to the language world without memory management, such as JAVA and C #, because these languages all have garbage collection mechanisms, software developers are no longer involved in memory management. However, these languages are at the cost of reducing the running performance of the software, because during garbage collection, all objects cannot run and need to wait until the memory is recycled. In addition, the statistics and marking of spam are also greatly increased, leading to system slowdown. In the world of C ++, is there any better solution to this problem? The answer is yes. It neither introduces the garbage collection mechanism nor increases the burden on resources. In the STL library, std: auto_ptr is introduced, which is a smart pointer.

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.