C ++ smart pointer

Source: Internet
Author: User

C ++ smart pointer
Introduction to C ++ smart pointer

Many powerful functions of C ++ rely on pointers for implementation. However, pointers are the most error-prone. The most error-prone point is that when an object has been released, the pointer is used to access its content, this causes invalid memory access errors.
In C ++ 11, a newSmart pointerType, which is used to manage dynamic objects. The behavior of smart pointers is similar to that of conventional pointers. However, it can automatically release the objects to avoid Memory leakage. In the new standard library, provides three types of smart pointers:
1. shared_ptr // allows multiple pointers to the same object,
2, unique_ptr // exclusive pointing to the specified object, cannot point to the same object.
3. weak_ptr // companion class is a weak reference pointing to the objects managed by share-ptr.
These three categories are stored in
# Include <memory> header file.

Performance_ptr class

Intelligent pointer template, we must provide the object type to which it points.

1. Define smart pointers:

Pai_ptr
  
   
P1; // defines a smart pointer to the string type. Pai_ptr
   
    
> P2; // defines a smart pointer to the string list type.
   
  

The smart pointer initialized by default stores a null pointer,
In condition determination, a smart pointer is used to check whether the pointer is a null pointer.

2. Determine whether the smart pointer points to NULL:

share_ptr
  
    p1;if(!p1){    cout<<"p1 is point to null"<
   
  
make_shared

1. Create a smart pointer:

Pai_ptr  P1 = make_shared  ("Test share point"); // use make_shared  Template Function to create a smart pointer. // You can also use the auto smart pointer to save auto p2 = make_shared  > ();    
Reference count

We can think that all objects pointed to by smart pointers have an associated counter.Reference count.
When we copy a shared_ptr, the reference count of the object to which it points will increase progressively, for example:
1. Use one shared_ptr to initialize another shared_ptr pointer;
2. Pass shared_ptr as a parameter to a function;
3. Use shared_ptr as the return value of the function;
In the following cases, the reference count will decrease:
1. If the shared_ptr variable is assigned a new value, the reference count of the object it points to is reduced by one.
2. If the shared_ptr variable is destroyed (such as out-of-scope), the reference count of the object to which it points will also be reduced by one.

When the reference count of a shared_ptr variable changes to 0, it automatically releases the object to which it points, instead of manually releasing it.

Auto a = make_shared  (100); // defines the smart pointer auto B = make_shared  (200); // defines the smart pointer B = a; // the pointer points to a new object.  

In the above Code, because the B pointer is assigned a new value, the reference calculation of the object it previously referred to will change from 1 to 0, and its object will be automatically released, the reference count of the object to which a points will change from 1 to 2.

Related Article

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.