C++11 shared_ptr & unique_ptr & Move semantics (rvalue reference)

Source: Internet
Author: User

just read it
smart_ptr:https://mbevin.wordpress.com/2012/11/18/smart-pointers/

the use of any pointers is to consider ownership+memory-management+lifetime.
Who was the owner of this object? Is there one owner or many? Who would responese for the deletion?

Using unique_ptr means that the object is owner but can be transferred (Std::move)

Plain pointers should never be used to act as the ' owners ' of objects

Use shared_ptr as much as possible, consider weak_ptr in ring references (does not increase the reference count of only needles)

Assign a value to shared_ptr or unique_ptr immediately after an object is created, and immediately assign a normal pointer to a smart pointer when the function returns a normal pointer to ensure automatic memory management

A smart pointer object is stored directly in container as a value, not as a pointer or reference, passing a function argument as a value, returning a function with a value (Std::move, rvalue Reference).

Using shared_ptr is multithreaded security, but shared_ptr itself is not multithreaded security. That is, when using smart pointers in multi-threading, each thread should have a shared_ptr object instead of multiple threads using the same shared_ptr object.


move:https://mbevin.wordpress.com/2012/11/20/move-semantics/

move and STD template container application:
To be continued ...

C++11 shared_ptr & unique_ptr & Move semantics (rvalue reference)

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.