Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ C ++ 11 smart pointers     - MNIST presentation and database conversion (Database)

- PLSQL Developer synchronization table tools (Database)

- VMWare virtual machine without rebooting way to add virtual disk (Linux)

- Oracle GoldenGate Installation and Configuration Tutorial Introduction (Database)

- Json Applications of FastJson (Programming)

- Gentoo: startx problem appears Failed to load module (Linux)

- About phpwind 5.01-5.3 0day analysis of the article (Linux)

- Compare Oracle MySQL (ICP) index condition pushdown is described (Database)

- MySQL 5.6.12 binary log path switching binlog (Database)

- Java call by value and by reference (Programming)

- php security study (Linux)

- Boost notes --Thread - problems encountered in the initial use on Ubuntu (Programming)

- Get and Post requests Comments (Linux)

- Axel install plug under CentOS 5/6 acceleration yum downloads (Linux)

- RHEL7 Apache MPM configuration (Server)

- Java 8 stream parsed into SQL (Programming)

- Linux regex sed detailing (Linux)

- Linux, MySQL / MariaDB Galera Cluster Setup Process (Database)

- Hadoop configuration ssh automation of automation (Server)

- Linux learning portal: CentOS 6.4 system kernel upgrade (Linux)

  C ++ 11 smart pointers
  Add Date : 2018-11-21      
  In C ++ 11, the introduction of smart pointers. There are: unique_ptr, shared_ptr, weak_ptr.
These three components is to use a pointer in the smart pointer boost scheme. Many have used the boost smart pointers friends, you can easily find off between them:

std boost Function
unique_ptr scoped_ptr exclusive object pointer, and the pointer to ensure that the life cycle of an object consistent with
shared_ptr shared_ptr can be shared pointer object can be assigned to shared_ptr or weak_ptr.
The object pointer at the end of the life cycle of all shared_ptr associated with the end of a strong reference.
weak_ptr weak_ptr it can not determine the referent of the life cycle, when referring to the referent, we need lock () to use a shared_ptr.
C ++ 11 will boost included in this set of standards.

Sample code is as follows:

// File test-1.cpp
#include < memory>
#include < iostream>

using namespace std;

int main ()
    unique_ptr up1 (new int (11));
    unique_ptr up2 = up1;! // compile time error [1]

    cout << * up1 << endl;
    unique_ptr up3 = move (up1); // [2]!
    cout << * up3 << endl;
    if (up1)
        cout << * up1 << endl;

    up3.reset (); // [3]!
    up1.reset ();

    shared_ptr sp1 (make_shared ( "Hello"));
    shared_ptr sp2 = sp1;
    cout << "* sp1:" << * sp1 << endl;
    cout << "* sp2:" << * sp2 << endl;
    sp1.reset ();
    cout << "* sp2:" << * sp2 << endl;

    weak_ptr wp = sp2; // [4]!
    cout << "* wp.lock ():" << * wp.lock () << endl;
    sp2.reset ();
    cout << "* wp.lock ():" << * wp.lock () << endl; // will run-time error!
    return 0;
// Compile command: g ++ -std = c ++ 11 test-1.cpp
[1]: unique_ptr copy assignment is prohibited, and always maintain a unique_ptr manage an object.
[2]: unique_ptr although can not be assigned, but you can () function through the transfer of ownership of the object move. Once the move (), the original up1 is no longer valid.
[3]: reset () allows unique_ptr early release pointer.
[4]: a weak_ptr by the shared_ptr constructor.

shared_ptr and weak_ptr

As in the following example:

shared_ptr < string> s1 (new string);
shared_ptr < string> s2 = s1;
weak_ptr < string> w1 = s2;

s1, s2, w1 ptr_manage a point object.
There shared_ref_count and weak_ref_count two domains are recorded with reference to the number of its shared_ptr weak_ptr in this object. This is very easy to do, just copy constructor and assignment of function rather quoted value plus 1, minus 1 to destructor. ptr_manage the ptr field contains the real object pointer address.

When shared_ref_cnt was reduced to 0, the object is automatically released ptr pointer points. When shared_ref_cnt with weak_ref_cnt have become 0 before the release ptr_manage object.
So since, as long as shared_ptr associated with the presence of the object exists. weak_ptr does not affect the lifetime of the object. When used weak_ptr to access the object, the object may have already been released, the first lock ().

When performing:

s1.reset ()
at this time:

shared_ref_cnt by two became minus 1.

Then execute:

s2.reset ()
at this time:

shared_ref_cnt has been reduced to a 0, ptr corresponding object has been released, ptr is cleared. At this time, ptr_manage still retained. Because w1 also need to refer to it.

In the final, w1 also destructed when:

ptr_manage in weak_ref_cnt is reduced to zero, and finally even ptr_manage are released.
- What is Unikernel? (Linux)
- Ubuntu 14.04 CodeBlocks configuration manually compile Boost 1.57.0 (Linux)
- Advanced Linux security settings (Linux)
- Gentoo: startx appeared Failed to load module Problem Solving (Linux)
- Ubuntu U disk do not have write privileges can only read but not write (Linux)
- Linux Basic Course: Install the software from source code (Linux)
- About Nginx 7 (Server)
- Making Linux root file system problems on-link library (Programming)
- Android use canvas board painting (Programming)
- Oracle RAC upgrade to problems encountered (Database)
- Distributed File System FastDFS deployment (Server)
- Troubleshooting Linux virtual machine device id and the letter of inconsistencies (Linux)
- Online booking shocking information leakage risk, you dare book the tickets online? (Linux)
- OpenSSH server configuration file for each Common Definition (Server)
- Oracle archive log summary (Database)
- Linux kernel RCU (Read Copy Update) lock Brief (Linux)
- Oracle table Access Control (Database)
- JavaScript in this usage (Programming)
- MySQL function: group_concat () function (Database)
- Android Touch message passing mechanism analysis (Programming)
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.