Enote notation Example (2)--pointer (1) Smart pointer

Source: Internet
Author: User

to know what a smart pointer is, first understand what is called "resource allocation is initialization"What RAII: Raii-resource acquisition is initialization, i.e. "resource allocation is initialized"As explained in C + + Primer, "by defining a class to encapsulate the allocation and release of resources, you can guarantee the correct release of resources"Core: C++98 provides a language mechanism: Object ( "! Value Semantics Object ") is out of scope, and destructors are called automatically" destructors are not called if an exception is thrown in the constructor. " The destructor is called only if the constructor is executed correctly and the build object succeeds . "Linux multithreaded Server Programming" P20:"I think RAII is the most important feature of the C + + language that distinguishes it from all other programming languages, and a programmer who does not understand RAII is not a programmer of a grid""Every explicit resource configuration action (for example, new) should be executed in a single statement, and the configured resources will be handed to the handle object (such as shared_ptr) immediately in the statement, and the program generally does not have a delete""shared_ptr is a powerful tool for managing shared resources, and it is important to avoid circular references, and the usual practice is that the owner continues to point to Child's Shared_ptr,child holding the weak_ptr that points to owner"P15 "in modern C + + programs, DELETE statements are generally not present, and resources (including complex objects themselves) are managed through objects (smart pointers or containers), without the programmer having to worry about it." Why RAII: 1) Prevent resource leaks (" note is" resource ", not just Memory "), and prevent other resource management problems (e.g. in memory management: Wild pointers, repeated releases, etc.)(to Liu Weipeng blog: How to ensure that the resources will be released (even if there is an exception), this in D corresponds to the scope (exit), in Java corresponds to the Finally, in C # corresponding to the scoped using. In short, a certain action (usually a resource release) must be executed regardless of how the current scope exits. )when there are multiple resources to release, the true power of Raii is revealed, generally, if a function requests n resources in turn2) Part of the exception safety

Why Smart Pointers : Memory Management : C + + requires programmers to manually manage memory, and new, delete still does not guarantee that memory is not compromised. And to some extent to avoid the destruction of the state of the object, see "Effective C + +" Item29

When : between New and delete:
1) Return2) Delete in the loop, preceded by a continue,goto,break3) Exception (exception is thrown in the code; a function that throws an exception is called; nested calls to the function that throws the exception) how : You have to be cautious when using new ( of course, new C + + is a good thing because I don't want to give up control of the system; not as c#,java as I do ) C, C + + is only a transitional product of the times ... 1) to write new and delete,a) first to be written in pairs, and secondly, b) to ensure that there are no such 3 cases between new and delete. 2) To consider the future of you, you modify someone else's code, others modify your code. When so add return,continue,goto,break, or call the function (only the calling function can throw an exception), consider the possible resource management problems "   What smart pointers : Smart pointers are implementations of the "RAII" technology. The core concept of smart pointers: The key features of effective C + + are: 1) The Resource Management object takes over immediately after the allocation of resources . 2) Resource management Object << through >> destructor to ensure correct resource release .  "more effective C + +" describes this: "Smart pointers is objects that is designed to Look,act,and feel like build-in pointers,b UT to offer greater functionality. They has a variety of applications, including resource management, as well as automatic duplicate write code work " smart Pointer Implementation features: Ownership transfer, ownership monopoly, reference counting 
"Effective C + +" P67What reference count : sometimes we want to keep resources until the last user ( an object ) is destroyed . When you copy a Raii object in this case, you should increment the "referenced number" of the resource.

above, the copyright belongs to the author, any form reproduced please contact the author. -------------------------------------------------------------------------- Two JPG versions of screenshots :     --------------------------------------------------------------------------
Note: All the content here is my original, the copyright of this article belongs to eversteins all, plagiarism must investigate.

Enote notation Example (2)--pointer (1) 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.