C + + Smart pointers

Source: Internet
Author: User

C + + Smart pointers

As we all know, the biggest difference between C + + and Java is that there is a very important tool in C + +-pointers. Analyticals defeat Xiao, the efficiency and flexibility of the pointer, but also caused the C + + complex and not easy to manage. Management of pointers Once there is a problem, the memory leaks and the system crashes. In order to deal with the memory leak caused by pointers, some so-called smart pointers have sprung up in C + +.

First, the implementation of STL Auto_ptr

Auto_ptr is essentially a further encapsulation of the base pointer type, which allows for a somewhat more secure management of these pointers.

The biggest and greatest advantage of auto_ptr is that it can effectively deal with the problem of abnormal interrupts, assuming that the program encounters an exception during the run, then the code behind will not be executed, including code that frees the memory that has been requested. Auto_ptr can solve this problem effectively. As shown in the following code.

class test{...    }; int Main () {  new  test ();   // some operation   here // But throws the exception  Delete t;     return 0 ;    }

As the code shows, the program encounters a memory leak scenario after a one-off event. Assuming that we use auto_ptr, this situation does not occur.

class test{...    }; int Main () {  auto_ptr<test> t (new  test ());   // some operation   here // But throws the exception  return 0 ;    }

Even if an exception occurs, the memory requested above can be safely released. Because the T object is placed on the stack, when it exits from the scope, its destructor is automatically called to release the memory object it manages (and this process also occurs)

Note that the above auto_ptr must be defined in a direct way (that is, by passing the argument to the parentheses immediately following the object name), because his constructor is explicit .

The above rules are very reasonable, but apart from the above provisions, the rest of the grammar, it makes people feel incredible.

First, replication and assignment are destructive. When we say that an object is assigned a value to another auto_ptr by invoking the copy constructor, it causes the original auto_ptr to be destroyed, that is, its base object pointer becomes an unbound state.

Auto_ptr<test> T (new Test ());auto_ptr<test> T1 (t);This is where we can test the binding state of Tif (T.get ())cout<< "NOT NULL" <<endl;Elsecout<< "NULL" <<endl;

The above get is used if T returns 0 for an unbound state, otherwise 1 is returned. Obviously the above code returns 0, and the output is null. Because in the copy operation, it has been reset to the binding state.

For an assignment operation, the same effect is true.

Second, release and reset

The immediate feeling is that release is releasing memory, but in fact it just sets the current smart pointer to an unbound state, and to free up memory you must use the following reset method.

Even after release, we can still call the object's reset to free up memory.

Auto_ptr<test> T (new  test ()); T.release (); // The memory is not releasedT.reset (); // The memory is released acutually

The above reset also has other features, such as the ability to point to a new memory pointer, which is also destructive.

Assuming you have just started auto_ptr without any memory binding, OK

If any of the pointers are bound, then it will be released first.

Formally based on the various inconveniences of auto_ptr, the share_ptr rise in the boost library.

Second, Boost shared_ptr

Refer to [1] for details.

[1] http://www.cnblogs.com/TianFang/archive/2008/09/19/1294521.html

C + + Smart pointers

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.